Re: LoadImages fails after X number of loads on same images

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
****

Any ideas on what going wrong?

Thanks.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: VB3 is making my head spin!
    ... > and displaying it pixel by pixel] tell me how. ... compressed bitmap from an original 24 but full colour bitmap that is held on disk as a standard ... It shows you how to load a full colour 24bit .bmp file from disk and count ... Dim z As Long, t1 As Long, t2 As Long ...
    (comp.lang.basic.visual.misc)
  • Re: After trying to load massive file, cant start Eclipse
    ... some sort of login bug there that prevents me from being able to post ... and it fails before I can do anything. ... IDE to load the file on startup, and if it can't load the file at all ... Removing the entire .metadata folder does work, ...
    (comp.lang.java.programmer)
  • Re: Loading image byte array
    ... Thanks Frank, I actually got it working myself before I saw your code. ... load the my image like Michael Phillips suggested. ... at that point Then create a Graphics using a fast-access second bitmap (we ... by pixel over to the byte array. ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: [SLE] eth0 now eth1 WTF? Network woes....The saga....[Long]
    ... > Then on a subsequent reboot the network doesn't come up. ... but the NIC fails to load and the error is "unable to load ... > On reboot, the network fails. ... dmesg says it is now trying to load eth1? ...
    (SuSE)
  • Re: ImageList: Is this possible?
    ... Now I want to load ... There's an API fiunction that lets you create a bitmap in memory: ... lpInfoHeader As BITMAPINFOHEADER, ByVal dwUsage As Long, lpInitBits As ...
    (microsoft.public.vb.general.discussion)