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 have a bitmap object and draw some curves on it by setpixel method.
I have a Format32bppArgb backbuffer, where I draw some lines: var g = Graphics.FromImage(bitmap);
I have to draw a String on a transparent bitmap at first, then draw
I have created a canvas and draw a bitmap on it, and animate it
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 downloaded images app in mobile. Every times an image is download
Possible Duplicate: Android: How to overlay-a-bitmap/draw-over a bitmap? I have implement the android Camera
I have to draw underlined-multiline text with all types of text alignment. I have

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.