System.Drawing.Bitmap.FromResource throwing System.ArgumentException 
Author Message
 System.Drawing.Bitmap.FromResource throwing System.ArgumentException

I am having trouble with System.Drawing.Bitmap.FromResource.  It takes an
IntPtr called hinstance, which is supposed to be a handle to a file
containing the desired bitmap resource.  If I use the following code, I get
an exception on the line calling FromResource():

FileStream fs = new FileStream(
    Application.ExecutablePath, FileMode.Open, FileAccess.Read,
FileShare.Read
);
Bitmap bmpFromResource = Bitmap.FromResource(fs.Handle, "image"); //
Exception thrown here.
Console.WriteLine(bmpFromResource.Height);

Unhandled Exception: System.ArgumentException: Invalid parameter used.
   at System.Drawing.Bitmap.FromResource(IntPtr hinstance, String
bitmapName)
   at BitmapSnippet.BitmapClass..ctor() in d:\bitmapclass.cs:line 358
   at EntryPoint.Main(String[] args) in d:\bitmapclass.cs:line 454

Questions:
- If a FileStream.Handle is inappropriate, where should I get the
appropriate instance handle to pass to this method?
- If FileStream.Handle is the right place to get hinstance, why isn't it
working?

Here's what I know:
- The Application.ExecutablePath points to the exe's own path & filename.
- The fact that the program is accessing its own exe does not seem to be the
problem because I tried copying the exe and pointing to the twin and I get
the same exception.
- The method in question is documented at
http://www.*-*-*.com/
frlrfSystemDrawingBitmapClassFromResourceTopic.asp
- The following code works in the same class, so I know the resource is in
the exe.

System.Resources.ResourceManager resources = new
    System.Resources.ResourceManager(typeof(BitmapClass)); // BitmapClass is
the name of this code's containing class.
Bitmap bmpFromResource = (Bitmap)(resources.GetObject("image"));
Console.WriteLine(bmpFromResource.Height);

Thanks.



Tue, 27 Jan 2004 09:15:45 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. System.Drawing.SafeNativeMethods threw an exception

2. Unhandled Exception: System.Configuration.ConfigurationException: Could not create System Configuration.NameValueSectionHandler, System

3. new Bitmap -- System.ArgumentException at runtime

4. System.Drawing.Design.UITypeEditor

5. Using efficiently System.drawing.design.imageEditor class

6. System.Drawing.SafeNativeMethods.GdipCreateFontFromLogfont fails

7. System.Drawing.Graphics.Clear(Color color) gives me a Compile Error

8. ? HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\Root\LEGACY_

9. System.Runtime.InteropServices.VTableCallsNotSupportedException

10. need help on _Module.UpdateRegistryFromResource()

11. System.InvalidCastException thrown when using a WebService

12. System.Timers.Timer can't throw Exception?

 

 
Powered by phpBB® Forum Software