![]() |
Selecting a bitmap into a DC
I need to take an image from a bitmap file and display it in my GUI. To load the image I use LoadImage with LR_LOADFROMFILE. Then I select it into a memory DC and finally call BitBlt to get it onto the screen.
My problem seems to be that when I select it into memory DC it does not get selected. I assume this because SelectObject is returning NULL. I cannot get any further information as SelectObject does not appear to set GetLastError (which is not mentioned in the documentation for SelectObject). I wonder if SelectObject will work on an object loaded with LoadImage. The documentation on SelectObject list the functions with which the GDI object must be created; LoadImage is not among them. However a bit of browsing comes up with loads of examples of objects created with LoadImage being selected into DCs. In any case, if LoadImage will not work what should I be using? I'm somewhat new to GDI, so any help would be appreciated. |
Re: Selecting a bitmap into a DC
Can you share the code you are having problem with.
Here is the simple one which loads an image and draws on the DC in the paint of the DialogBased app In InitDialog Code:
m_hBmp = ::LoadImage(NULL,"E:\\flowers.bmp",IMAGE_BITMAP,NULL,NULL,LR_LOADFROMFILE|LR_DEFAULTSIZE);Code:
CDC dcSrc; |
Re: Selecting a bitmap into a DC
It's scattered around the code but here are the relevant bits:
In the constructor Code:
mnubmp = (HBITMAP) LoadImage (0, bmpsrc, IMAGE_BITMAP, bmplen, height, LR_LOADFROMFILE);In the draw method Code:
HDC hdcMem = CreateCompatibleDC (dis->hDC); |
Re: Selecting a bitmap into a DC
Have you tried my one.
|
Re: Selecting a bitmap into a DC
Same problem!!! Is there something wrong with my system?
Whew! First time using MFC. It's so much more complicated than the old fashoined way. |
Re: Selecting a bitmap into a DC
Quote:
|
Re: Selecting a bitmap into a DC
Yes. The debugger reveals that LoadImage is working fine. It's SelectObject that isn't.
|
Re: Selecting a bitmap into a DC
Check the error with GetLastError
|
Re: Selecting a bitmap into a DC
Ok, it appears SelectObject does set GetLastError. My mistake.
The first time the draw methos is called CreateCompatibleDC set GetLastError to 0 (success). On subsequent calls it sets it to 87 (invalid parameter). However it still returns a non NULL HDC. SelectObject consistently sets GetLastError to 0, but returns a NULL HGDIOBJ. Curiouser and curiouser. What's going on here. |
Re: Selecting a bitmap into a DC
I should point out that I'm releasing all unused DCs, so I don't think memory leaks are the problem. Here's the code (from the draw method).
Code:
SelectObject (hdcMem, hbmpOld); |
| All times are GMT +5.5. The time now is 08:56. |