using CImage:.but LoadFromResource ()failed  
Author Message
ironox





PostPosted: Visual C++ General, using CImage:.but LoadFromResource ()failed Top

IDR_GIF1 is a  gif imported from resource editor.

and GIF is custom resource!

m_image.LoadFromResource(AfxGetInstanceHandle(),IDR_GIF1);//load failed!

 if (!m_image.IsNull())
 {
 
  m_image.BitBlt(dc,0,0);
 }

i also tried :

HINSTANCE hInst = AfxGetResourceHandle();
 HRSRC hRsrc = ::FindResource(hInst,
  MAKEINTRESOURCE(IDR_GIF1),
  TEXT("GIF")); // type
 if (!hRsrc)
  return FALSE;

 // load resource into memory
 DWORD len = SizeofResource(hInst, hRsrc);
 BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
 if (!lpRsrc)
  return FALSE;

 // create memory file and load it
 CMemFile file(lpRsrc, len);
 m_image.Load(file.GetFilePath());
 FreeResource(hRsrc);
 file.Close();

but still i got the same result :FAILED!

need you help:)

 

 




Visual C++8  
 
 
nobugz





PostPosted: Visual C++ General, using CImage:.but LoadFromResource ()failed Top

CImage::LoadFromResource() is a very thin wrapper around the LoadImage API function. Be sure to call GetLastError() so you have an idea why it might be failing.


 
 
gdrivas





PostPosted: Visual C++ General, using CImage:.but LoadFromResource ()failed Top

Do you call this function from whithin a dll

if you do so, you must include the following code at the very first of the spesific code segment:

AFX_MANAGE_STATE(AfxGetStaticModuleState( ))


 
 
ironox





PostPosted: Visual C++ General, using CImage:.but LoadFromResource ()failed Top

thanks!

i'm not calling this function from within a dll:)

it seems that LoadImage can only load bitmaps ,icons and cursors.


it's true LoadFromResource is a thin wrapper of ::LoadImage,


so why can it load a gif image!


i'm more and more confused!


BTW where should I put the GetLastError()

because it is m_image.LoadFromResource(AfxGetInstanceHandle(),IDR_GIF1);
that cause a assert failed.

if i put GetLastError() behind m_image.LoadFromResource(AfxGetInstanceHandle(),IDR_GIF1);

it will not get a chance to be executed.

thanks for your help




 
 
nobugz





PostPosted: Visual C++ General, using CImage:.but LoadFromResource ()failed Top

Couple of things I noticed when trying this out; first off: CImage::LoadFromResource can only load true bitmap resources, not custom resources. Note the IMAGE_BITMAP argument it uses when calling ::LoadImage(). Also, when I tried to add a .gif file resource with the VS2005 resource editor, I noticed that the editor automatically converted the .gif in a .bmp. Check if this happened to you too: open the resource in the editor and check the binary data, it is a .bmp if it starts with "BM".


 
 
ironox





PostPosted: Visual C++ General, using CImage:.but LoadFromResource ()failed Top

nobuz,thanks for your kind help!

you are right. CImage::LoadFromResource can only load true bitmap resources,not custom resources.

but CImage can truely load gif or jpg image into our application.

maybe there are other ways to do this,but not
CImage::LoadFromResource .








 
 
nobugz





PostPosted: Visual C++ General, using CImage:.but LoadFromResource ()failed Top

I sunk my teeth into this one. The CImage::Load(IStream*) looked appealing. What was needed was an IStream object that could read a resource. Check out the code below for the CResourceStream class. With this, you can now load your .GIF image from a resource like this:

CImage mImage;
...
CResourceStream str(0, MAKEINTRESOURCE(IDR_GIF1), _T("GIF"));
mImage.Load(&str);

Here's the code: