Go4Expert

Go4Expert (http://www.go4expert.com/)
-   MFC (http://www.go4expert.com/forums/mfc/)
-   -   Selecting a bitmap into a DC (http://www.go4expert.com/forums/selecting-bitmap-dc-t2518/)

ever_thus 11Jan2007 00:52

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.

shabbir 11Jan2007 12:27

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);
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.

ever_thus 11Jan2007 21:16

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);
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.

shabbir 11Jan2007 21:37

Re: Selecting a bitmap into a DC
 
Have you tried my one.

ever_thus 11Jan2007 22:09

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.

shabbir 12Jan2007 09:29

Re: Selecting a bitmap into a DC
 
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.

ever_thus 12Jan2007 10:54

Re: Selecting a bitmap into a DC
 
Yes. The debugger reveals that LoadImage is working fine. It's SelectObject that isn't.

shabbir 12Jan2007 14:54

Re: Selecting a bitmap into a DC
 
Check the error with GetLastError

ever_thus 12Jan2007 19:40

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.

ever_thus 12Jan2007 19:43

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);
SelectObject (hdcMem, brushOld);
DeleteObject (brush);
DeleteDC (hdcMem);



All times are GMT +5.5. The time now is 07:32.