.NET Framework Bookmark and Share   
 index > Common Language Runtime > Image.FromHbitmap exception: A generic error occurred in GDI+.
 

Image.FromHbitmap exception: A generic error occurred in GDI+.

Hi,All

I am using theuser32.dll API toload theimages from a dll resource file.
When the programruns, an exception turns up: A generic error occurred in GDI+.

I already search the web and this Forum.

There is a question verylike whatI met, but the language islike VBI can'tunderstand it well at http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/662dd6ed-0ec5-4c9b-bba1-0c049b6d3fe2/
Could somebody help me? thanks a lot

The code is as follows:

public static Image LoadImage(int Identifier, string ResourceFile)
{
// Works
IntPtr MyLib = LoadLibrary(ResourceFile);
IntPtr HShell32Image = LoadImage(MyLib, new IntPtr(Identifier), 0, 0, 0, 0);

Image myImage = Image.FromHbitmap(HShell32Image); //------------> exception from here
return myImage;
}

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr LoadImage(IntPtr hinst, IntPtr lpszName, uint uType,
int cxDesired, int cyDesired, uint fuLoad);

[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
public static extern IntPtr LoadLibrary(string lpFileName);




the same problem with Y.tks
shoucal
Hello shoucal

I tested your code upon a native dll with image resources, and did not reproduce the problem.

Could you please debug the code and check the value of HShell32Image ? If it's null, it means that some error occured in LoadImage. You can get the error code by changing LoadLibrary signature to

[System.Runtime.InteropServices.DllImport("Kernel32.dll", SetLastError=true)]
public static extern IntPtr LoadLibrary(string lpFileName);

and calling Marshal.GetLastWin32Error().

Another suggestion is to debug into Image.FromHbitmap. Its source code is available at
http://referencesource.microsoft.com/
Please configure your Visual Studio for debugging .NET source code:
http://referencesource.microsoft.com/serversetup.aspx

Regards,
Jialiang Ge

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Marked As Answer byshoucal Monday, September 21, 2009 3:19 AM
  •  
Jialiang Ge [MSFT]
Well, the function takes only one argument so you know there's something wrong with the argument. I bet that it is 0. Add some basic error checking to this code:

IntPtr MyLib = LoadLibrary(ResourceFile);
if (MyLib == IntPtr.Zero) throw new ArgumentException("Resource file not found");
IntPtr HShell32Image = LoadImage(MyLib, new IntPtr(Identifier), 0, 0, 0, 0);
if (HShell32Image == IntPtr.Zero) throw new ArgumentException("Resource ID not available");


Hans Passant.
nobugz
Hello shoucal

I tested your code upon a native dll with image resources, and did not reproduce the problem.

Could you please debug the code and check the value of HShell32Image ? If it's null, it means that some error occured in LoadImage. You can get the error code by changing LoadLibrary signature to

[System.Runtime.InteropServices.DllImport("Kernel32.dll", SetLastError=true)]
public static extern IntPtr LoadLibrary(string lpFileName);

and calling Marshal.GetLastWin32Error().

Another suggestion is to debug into Image.FromHbitmap. Its source code is available at
http://referencesource.microsoft.com/
Please configure your Visual Studio for debugging .NET source code:
http://referencesource.microsoft.com/serversetup.aspx

Regards,
Jialiang Ge

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Marked As Answer byshoucal Monday, September 21, 2009 3:19 AM
  •  
Jialiang Ge [MSFT]
Thanks,It is IntPtr.Zero.

the same problem with Y.tks
shoucal

Thanks,
It seems like the LoadLibray returns a Zero value.
I use the LoadLibray on different dlls, and find it works don't rentrun the expected value.
So, is there anything could effect the LoadLibary function works?

I will try it tomorrow.


the same problem with Y.tks
shoucal
Well, the function only takes one argument so you know there's something wrong with the argument. An obvious candidate is getting the path to the DLL wrong. Not being able to load the dependencies for the DLL is another common problem. Change the P/Invoke declaration to:

[System.Runtime.InteropServices.DllImport("Kernel32.dll", SetLastError = true)]
public static extern IntPtr LoadLibrary(string lpFileName);

Then modify your code to use it like this:

IntPtr MyLib = LoadLibrary(ResourceFile);
if (MyLib == IntPtr.Zero) throw new Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
IntPtr HShell32Image = LoadImage(MyLib, new IntPtr(Identifier), 0, 0, 0, 0);
if (HShell32Image == IntPtr.Zero) throw new ArgumentException("Resource ID not available");
Hans Passant.
nobugz

You can use google to search for other answers

Custom Search

More Threads

• Convert 'Windows Application' to 'Windows Service' .... imposible?
• Getting System.Type from static method
• I need an "IsFinalizeSuppressed" method
• Application locking on exit during Invoke
• Consuming .NET events in VBScript
• 'struct' value type?!?
• Framework compatibility
• How about constructors that are selfaware?
• COM Interop and AppDomain.CreateDomain
• call .net framework 3.5 dll from framework 2.0 application