Selecting a bitmap into a DC

ever_thus's Avatar, Join Date: Jan 2007
Contributor
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.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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);
In Paint
Code:
CDC dcSrc;
dcSrc.CreateCompatibleDC(NULL);
dcSrc.SelectObject(m_hBmp);
dc.StretchBlt(0,0,100,100,&dcSrc,0,0,100,100,SRCCOPY);
Of course the 0,0,100,100 should be adjusted with your rect.
0
ever_thus's Avatar, Join Date: Jan 2007
Contributor
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);
where bmpsrc is a char* parameter. All other variables are instance variables with valid values. LoadImage returns a valid handle.

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);
where dis is a DRAWITEMSTRUCT* parameter and brush is a HBRUSH parameter. All other variables are instance variables with valid values. mnubmp retains its valid value. CreateCompatibleDC return a valid handle; the first SelectObject returns NULL.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Have you tried my one.
0
ever_thus's Avatar, Join Date: Jan 2007
Contributor
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.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by ever_thus
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.
My one is working perfectly fine. Have you changed the url to the bmp file correctly.
0
ever_thus's Avatar, Join Date: Jan 2007
Contributor
Yes. The debugger reveals that LoadImage is working fine. It's SelectObject that isn't.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Check the error with GetLastError
0
ever_thus's Avatar, Join Date: Jan 2007
Contributor
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.
0
ever_thus's Avatar, Join Date: Jan 2007
Contributor
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);