Re: LoadImages fails after X number of loads on same images
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Fri, 19 Dec 2008 15:17:09 -0500
See below...
On Fri, 19 Dec 2008 10:43:01 -0800, mircowhat <mircowhat@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
A little background can be found in the link below on what i'm doing.****
http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?query=loadimage+fails&dg=microsoft.public.vc.mfc&cat=en_US_f0847235-3112-4b9a-8717-b82f7898eeea&lang=en&cr=US&pt=&catlist=&dglist=&ptlist=&exp=&sloc=en-us
My problem is that after say 20 trys(It's triggered by a button press),
Loadimage has a valid path for a image but it fails to load it. It also
varies. Sometime it will fail every time after that and sometimes it will
work on the next iteration.
Here is the code:
void CWCStatic::SetBitmap(char *imagepath)
{
//CAqtestDlg *asdf=(CAqtestDlg *)this->GetParent();
HANDLE hBmp
=LoadImage(NULL,imagepath,IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
This loads a bitmap. That's what it is supposed to do. Where you delete the bitmap? How
big is the bitmap?
Also, remember to to spaces around binary operators like | so the code is readable
****
if (hBmp == NULL){****
AfxMessageBox("setbitmap failed");
AfxMessageBox(imagepath);
//asdf->error("LoadImage");
CHAR szBuf[80];
DWORD dw = GetLastError();
sprintf(szBuf, "loadimage failed: GetLastError returned %u\n",dw);
NEVER use sprintf for this purpose, and NEVER use fixed-size buffers, and STOP using the
obsolete 8-bit character type. See my essay on FormatMessage on my MVP Tips site. The
CORRECT code would say
CString buf;
buf.Format(_T("LoadImage failed: GetLastError returned %u 0x%08x"), dw, dw);
AfxMessageBox(buf);
the char datatype is dead, the use of fixed-size buffers is dead, and the use of sprintf
is beyond all question totally dead. These are now merely considered bad programming
habits
****
AfxMessageBox(szBuf);****
//return;
}
CStatic::SetBitmap((HBITMAP)hBmp);
}
GetLastError returns 0, so nothing really useful there.
This function only loads a background image. Their is about 20
CBitmapButtons that also use a subclass using this same line "below" to load
the images. All of the Buttons still load fine when this happens.
"HANDLE hBmp
=LoadImage(NULL,imagepath,IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);"
The OS doesn't seem to be running out of Handles,"Verified through task
manager" so i'm not sure what the issue is.
Running out of GDI space is the most likely cause.
Why are you storing images in files instead of in the resource segment?
joe
****
Joseph M. Newcomer [MVP]
Any ideas on what going wrong?
Thanks.
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: LoadImages fails after X number of loads on same images
- From: mircowhat
- Re: LoadImages fails after X number of loads on same images
- References:
- LoadImages fails after X number of loads on same images
- From: mircowhat
- LoadImages fails after X number of loads on same images
- Prev by Date: Re: Help with CStringArray in Unicode, Multi-byte, & MFC
- Next by Date: Re: recurring mouse click messages
- Previous by thread: LoadImages fails after X number of loads on same images
- Next by thread: Re: LoadImages fails after X number of loads on same images
- Index(es):