-
Problem With My Bitmap
Hi, I have a bitmap located in the same directory as my program.
I compiled it successfully with my resource editor, specifying the identifyer as "PICTURE".
Then, in the program I declared a new HBITMAP variable "pic".
In "case WM_PAINT:" I did:
pic = LoadBitmap(hInst,"PICTURE");
if(!pic) {MessageBox(hwnd, "Failed To Load The Bitmap.", "Error",
MB_OK | MB_ICONEXCLAMATION);
return -1;
}
When the program starts up, the message box pops up, and of course, no bitmap!
Can anyone spot my mistake?
-
it has been awhile since i have displayed a bitmap in any of my programs, but if memory serves me correctly i think you need to have a two hdc handles. i would put that code you have in a WM_CREATE message along with this
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
hBitmap = LoadBitmap (hInst, TEXT ("PICTURE")) ;
GetObject (hBitmap, sizeof (BITMAP), &bitmap) ;
cxSource = bitmap.bmWidth ;
cySource = bitmap.bmHeight ;
and then in your WM_PAINT message have
hdc = BeginPaint (hwnd, &ps) ;
hdcM = CreateCompatibleDC (hdc) ;
SelectObject (hdcM, hBitmap) ;
for (y = 0 ; y < cyClient ; y += cySource)
for (x = 0 ; x < cxClient ; x += cxSource)
{
BitBlt (hdc, x, y, cxSource, cySource, hdcM, 0, 0, SRCCOPY) ;
}
DeleteDC (hdcM) ;
EndPaint (hwnd, &ps) ;
that is some code out of an old project i did. it should work fine.
oh you may need a WM_SIZE message or else your picture will size to 0 so add this
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
anyways i hope i helped.
-
I honestly did this just like two minutes ago. this is how i did it (I couldn't get LoadBitmap to work for me.
Code:
pic = (HBITMAP) LoadImage(hInst, "picture.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
-
Change your nickname. You are screwing up this message boards window. Your name is too big.
-
Try this....
Try putting the picture.bmp in your res folder, or the same folder that your icons are stored into. Maybe that'll work.
-
Hmm, LoadImage() is working in the sense that the message box does not pop up telling me it failed to load.
But still no image.
Thanks for getting me a little closer!
Also, I might add, I really hate the fact that all images must be compiled into a resource before using such functions as LoadBitmap(). I really love the fact that LoadImage() can directly grab the file! Awesome.
-
I THINK I'M ALMOST THERE!
Here's what I have so far:
Code:
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HBITMAP ball;
HDC hdc = NULL;
HDC compat_hdc;
PAINTSTRUCT ps;
RECT rc;
switch (message)
{
case WM_CREATE:
UpdateWindow(hwnd);
if( ball = (HBITMAP) LoadImage(hin, "ball.bmp", IMAGE_BITMAP, rc.right, rc.bottom, LR_LOADFROMFILE) == 0)
{
MessageBox(hwnd, "Load of resources failed.", "Error",
MB_OK | MB_ICONEXCLAMATION);
}
if(GetClientRect(hwnd, &rc) == 0)
{
MessageBox(hwnd, "GetClientrect failed.", "Error",
MB_OK | MB_ICONEXCLAMATION);
}
if( (compat_hdc = CreateCompatibleDC(hdc)) == NULL)
{
MessageBox(hwnd, "CreateCompatibleDC failed.", "Error",
MB_OK | MB_ICONEXCLAMATION);
}
if(SelectObject(chdc,ball) == 0)
{
MessageBox(hwnd, "Selection failed.", "Error",
MB_OK | MB_ICONEXCLAMATION);
}
if(BitBlt(hdc,0,0,10,10,chdc,0,0,SRCCOPY) == 0)
{
MessageBox(hwnd, "Blit failed.", "Error",
MB_OK | MB_ICONEXCLAMATION);
}
DeleteObject(ball);
DeleteDC(chdc);
Now the failure is only during the BitBlt() operation! Any Ideas?
-
Okay. I really don't get it!
I changed the first parameter of BitBlt() to "compat_hdc" instead of "hdc" ( Shoulda done that in the first place!), and changed the rest to look like this:
BitBlt(compat_hdc,0,0,rc.right,rc.bottom,compat_hd c,0,0,SRCCOPY)
...and no more error message boxes pop up!
So you'd think I'd have the image by now, but not so!
Anyway, that's where I'm at.
-
Have you tried MAKEINTRESOURCE?
ie
LoadBitmap(hInst,MAKEINTRESOURCE(PICTURE));
Get a DC with GetDC()
Created a CompatibleDC()
Created a bitmap with CreateCompatibleBitmap()
or load the bitmap into the hdc with SelectObject()?
Then StretchBlt() or BitBlt() it to the hdc?
BeginPaint returns one or use the one inside the PAINTSTRUCT.
Code:
hdc=GetDC(hWnd);
hdcFrame=CreateCompatibleDC(hdc);
hFrameBMP=CreateCompatibleBitmap(hdc,ClientRect.right-ClientRect.left,ClientRect.bottom-ClientRect.top);
hSysFrameBMP=SelectObject(hdcFrame,hFrameBMP);
ReleaseDC(hWnd,hdc);
Remember to put the hdc's back EXACTLY as you found them or lose GDI resources.
Nothing should be done in a paint function except BeginPaint()
a BitBlt() or if you HAVE to a StretchBlt()
EndPaint()
everything else shoould be done to a buffer and them copied to the frame at the last minute. THEN call paint with an UpdateWindow() or InvalidateRect() to copy to the screen.
-
Having looked closer at your code you are not 'Getting' a DC but creating a compatible to NULL one.
Try GetDC() on the handle of the control/dialog you want to draw on.