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.
|
Go4Expert Founder
|
![]() |
| 11Jan2007,12:27 | #2 |
|
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; dcSrc.CreateCompatibleDC(NULL); dcSrc.SelectObject(m_hBmp); dc.StretchBlt(0,0,100,100,&dcSrc,0,0,100,100,SRCCOPY); |
|
Contributor
|
|
| 11Jan2007,21:16 | #3 |
|
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); HGDIOBJ hbmpOld = SelectObject (hdcMem, mnubmp); HGDIOBJ brushOld = SelectObject (hdcMem, brush); FillRect (dis->hDC, &dis->rcItem, (HBRUSH) brush); ExtFloodFill (hdcMem, 0, 0, GetPixel (hdcMem, 0, 0), FLOODFILLSURFACE); BitBlt (dis->hDC, dis->rcItem.left + ((maxbmplen + BMP_PADD - bmplen) /2), dis->rcItem.top + ((dis->rcItem.bottom - dis->rcItem.top - height) / 2),maxbmplen, height, hdcMem, 0, 0, SRCCOPY); |
|
Go4Expert Founder
|
![]() |
| 11Jan2007,21:37 | #4 |
|
Have you tried my one.
|
|
Contributor
|
|
| 11Jan2007,22:09 | #5 |
|
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. |
|
Go4Expert Founder
|
![]() |
| 12Jan2007,09:29 | #6 |
|
Quote:
Originally Posted by ever_thus |
|
Contributor
|
|
| 12Jan2007,10:54 | #7 |
|
Yes. The debugger reveals that LoadImage is working fine. It's SelectObject that isn't.
|
|
Go4Expert Founder
|
![]() |
| 12Jan2007,14:54 | #8 |
|
Check the error with GetLastError
|
|
Contributor
|
|
| 12Jan2007,19:40 | #9 |
|
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. |
|
Contributor
|
|
| 12Jan2007,19:43 | #10 |
|
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); SelectObject (hdcMem, brushOld); DeleteObject (brush); DeleteDC (hdcMem); |

