Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5991435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:23:09+00:00 2026-05-22T23:23:09+00:00

I have to draw a bitmap multiple times. It’s loaded from file. I can

  • 0

I have to draw a bitmap multiple times. It’s loaded from file. I can reload it every time I have to use it in SelectObject the following way:

void drawBitmap(HWND hWnd, int xPos, int yPos) {
    HBITMAP hBmp = (HBITMAP) LoadImage(NULL, "image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    HDC hDC = GetDC(hWnd);
    HDC hdcMem = CreateCompatibleDC(hDC);
    SelectObject(hdcMem, hBmp);
    BitBlt(hDC, xPos, yPos, 7, 7, hdcMem, 0, 0, SRCCOPY);
}
drawBitmap(hMainWnd, 0, 0);
drawBitmap(hMainWnd, 14, 0);
drawBitmap(hMainWnd, 28, 0);

But is it also possible to do something like this?

HBITMAP hBmp = (HBITMAP) LoadImage(NULL, "image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
void drawBitmap(HWND hWnd, int xPos, int yPos) {
    HBITMAP hBmp2 = hBmp;
    HDC hDC = GetDC(hWnd);
    HDC hdcMem = CreateCompatibleDC(hDC);
    SelectObject(hdcMem, hBmp2);
    BitBlt(hDC, xPos, yPos, 7, 7, hdcMem, 0, 0, SRCCOPY);
}
drawBitmap(hMainWnd, 0, 0);
drawBitmap(hMainWnd, 14, 0);
drawBitmap(hMainWnd, 28, 0);

But this only draws one bitmap…

MSDN says:

The SelectObject function selects an
object into the specified device
context (DC). The new object replaces
the previous object of the same type.

So maybe my hBmp is wasted after SelectObject is called. But I copied it into hBmp2 first, then what’s the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:23:10+00:00Added an answer on May 22, 2026 at 11:23 pm

    You are not deleting the memory DC when you are done with it. That means the DC is leaked, and the bitmap is still selected in that leaked DC. And according to the SelectObject documentation: “An application cannot select a single bitmap into more than one DC at a time.”

    So the second SelectObject fails because the bitmap is still selected in the first HDC.

    Clean up after yourself by calling DeleteDC at the end of the drawBitmap function (and also call DeleteObject on the hBmp when you are done with it).

    Additionally, the HBITMAP hBmp2 = hBmp; line accomplishes nothing. You’re just assigning the handle to a different variable. It’s still the same handle to the same bitmap.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to draw an image from .png resource file. I have tried
I have to draw a String on a transparent bitmap at first, then draw
I'd like to draw antialiased text on a transparent bitmap and have the antialiasing
I am trying to composite a background bitmap from multiple smaller bitmaps. I am
basically i am reading image bytes from file i have a loop which has
I have a drawing app where the user can draw lines with their finger,
I have a mapping application that needs to draw a path, and then display
I have a line that I draw in a window and I let the
I have some code in .Net to draw some text content using GDI+. I
I have a SWF Video player on my webpage. I want to draw a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.