Win32 loading BITMAPS

Started by
4 comments, last by FxMazter 21 years, 4 months ago
Hia! I have just started out with Win32 Programming. Ok, I''m stuck on loading bitmaps with GDI. This is how the function for drawing bitmaps looks: ------------------------------------------------------------- int DrawBitmapResource(HDC hDestDC, int xDest, int yDest, int nResID) { HBITMAP hBitmap; HDC hSrcDC; BITMAP bmp; int nWidth, nHeight; if((hBitmap = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(nResID), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION)) == NULL) return(FALSE); if((hSrcDC = CreateCompatibleDC(NULL)) == NULL) return(FALSE); if(SelectObject(hSrcDC, hBitmap) == NULL) return(FALSE); if(GetObject(hBitmap, sizeof(BITMAP), &bmp)) return(FALSE); nWidth = bmp.bmWidth; nHeight = bmp.bmHeight; if(BitBlt(hDestDC, xDest, yDest, nWidth, nHeight, hSrcDC, 0, 0, SRCCOPY) == NULL) return(FALSE); DeleteDC(hSrcDC); return(TRUE); } ------------------------------------------------------------- and this is how I call for the function: ------------------------------------------------------------- DrawBitmapResource(hMainDC, 0, 0, 1002); ------------------------------------------------------------- hMainDC //a Device Context for the main window hInstance //handle to the main window 1002 //resource id for my bmp picture the problem is that when I try this, no errors occure... but the bitmap isn''t shown either! (black screen is shown) Could someone tell me what I have made wrong? THX!
Advertisement
SelectObject returns GDI_ERROR on failure. you''re if test will return on success if no other bitmap object was selected into the device context you just created (which i think is not the case for DCs just created via CreateCompatibleDC, but the test is still incorrect.)

GetObject returns zero on failure. you''re if test will return on success.


and just some side notes:

i would replace LR_CREATEDIBSECTION with LR_DEFAULTCOLOR. just creating a DIB here does nothing for you since you only use the bitmap once. it''s best to go ahead and let LoadImage convert the bitmap to a DDB. this will make the blit faster. a DIB is more of a special bitmap that is useful only under certain conditions. the the performance difference between blitting a DIB and a DDB is significant, since the DIB must first be converted to a DDB.

i would also replace the NULL in the CreateCompatibleDC call with hDestDC, since that is the true device context you''re eventually blitting to. there''s no guarantee from within the scope of this function that the screen DC (what the NULL is used to represent) has the same display attributes as hDestDC.

this function is leaking gdi resources all over the place. you create a bitmap and a DC, neither of which are deleted. you also select the bitmap into the DC but never restore the previously selected bitmap, if any, to the DC. look up DeleteObject, DeleteDC, and SelectObject in MSDN for more on what i''m talking about.
Hmm, I''m not hanging in here 100% but. I don''t see where is leaks GDI all over ? hmm...

To me it seems that I first create a handle to the bitmap (this doesnt need to be deleated right ?)

Then I create a Device context for the Bitmap right? (which is deleated in the end of the function right?)

But before it is deleated the bitmap is selected and copied to the Device context for the main window right? (hDestDC is the hMainDC)

So there shouldn''t be any memory leakage right?



all of your assumptions about not having to do this or that are incorrect for the api calls you''re using. maybe you''re thinking of GDI+ or MFC or another class based system.
Hell, of course
This is no API like DX or something... this is pure GDI, at least the text I''m reading sais so. Just started out with Win32 programming.
Well, anyway... I did solve this problem. It was:

if(GetObject(hBitmap, sizeof(BITMAP), &bmp))
return(FALSE);

no matter what, it returned false

if(GetObject(hBitmap, sizeof(BITMAP), &bmp) == 0)
return(FALSE);

is how it should be


But hey, feel free to check my other post about fonts ^^

thx!

This topic is closed to new replies.

Advertisement