System.InvalidCastException in System.Drawing.dll when loading Bit

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

My app loads a bunch of bitmaps, stored on the device as separate .bmp files.
I use this simple code to load them:

Bitmap bmp = new Bitmap(path);

For most of these bitmaps, I an exception is thrown (and internally catched)
in System.Drawing.dll:

A first chance exception of type 'System.InvalidCastException' occurred in
System.Drawing.dll

I've been trying to understand why this would happen on some bitmaps and not
on others. All the .bmp files are valid (created with Macromedia Fireworks)
and small in size (from 16x16, 20x20, 32x32 for most of them).

Any lead as to what's internally happening?
 
More information on this issue:

I stepped into the System.Drawing code and here is where the exception is
occuring:

{System.InvalidCastException: InvalidCastException
Microsoft.AGL.Common.MISC.CreateMemoryStream()
System.Drawing.Bitmap..ctor()

Note that this is with .NETCF 2.0 on a PocketPC 2003 device.
 
Hi there,
I have the exact same problem with CF2.0, what has worked for me is embedding
the image and retrieving it like this:

Bitmap bmp = new Bitmap(Assembly.GetExecutingAssembly().
GetManifestResourceStream("YourAppName.Resources." + bitmapFileName));

I hope this helps..
 
Thanks for your answer. Actually this technique is not an option as the
client explicitely asked for the bitmaps to be stored separate from the app
so they can upgrade them without having to rebuild it.

I'd still like to figure out what's going on, thought it's good to know that
once embedded, the issue won't happen again. I assume that when Visual Studio
embeds pictures in resources, it pre-chews them so they are in the correct,
expected format or whatever.
 
I too have felt this pain. There is actually no issue here, you most likely just have the VS setting "break on all thrown CLR exceptions" (under Debug | Exceptions). Simply turn that off and your problem goes away.
 
Back
Top