Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi,

i run the following lines of code on a Mobile Device with CE6.0

The Bitmap is shown proper in the Picturebox.
I tried also jpg and png Format with same Result.

The Debugger stops only at the Exception if i activate.
Debugging->Common Language Runtime-> System -> System.InvalidCastException
Without this it will Show it only as Exception (first Chance) in the Output window and the code runs like expected.


Does anybody know how to prevent against this Exception or how to find the reason for it?


C#
try
{
  System.Windows.Forms.PictureBox picturebox = new System.Windows.Forms.PictureBox();
 
  // this line causes the InvalidCastExeption
  // the Bitmap is available and shown proper in the Picturebox.
  // the Debugger stop only in this line if i activate
  // Debugging->Common Language Runtime-> System -> System.InvalidCastException
  // but it jumps over the two catch blocks
  picturebox.Image = new System.Drawing.Bitmap("Bitmap.bmp");
 
  this.Form1.Controls.Add(picturebox);
}
catch (System.InvalidCastException ice)
{
  System.Console.WriteLine(ice.Message);
}
catch (System.Exception ex)
{
  System.Console.WriteLine(ex.Message);
}


thank's
de-kekse
Posted
Updated 17-Nov-15 8:19am
v2
Comments
Wombaticus 17-Nov-15 12:46pm    
Can't you add some error catching to find out?
Don't you need to give the full path to the picture file location, not a relative one?
Richard Deeming 17-Nov-15 12:52pm    
Your code doesn't compile - you're missing the new from the second line:

picturebox.Image = new System.Drawing.Bitmap("Bitmap.bmp");

1 solution

A "first chance exception" in the output window is not something you need to worry about. It's an exception which has been thrown and caught within the framework code.

Since your code is running as expected, you can safely ignore the first-chance exception.
 
Share this answer
 
Comments
de-kekse 17-Nov-15 16:32pm    
Thank you Richard.
My real issue is here to find out why the "first Chance exception" is thrown.
I know my code is running with this exception, but the Debugger must know more than myself. Because i do not know why it is thrown.
And i would like to find out about the reason and will be happy with each comment that brings me closer to the knowledge of the Debugger.
Richard Deeming 18-Nov-15 8:04am    
The first chance exception is something that is thrown, caught, and handled entirely within the framework classes. It's effectively an "expected" exception.

There could be many reasons for the exception to be thrown, none of which you need to worry about.

If you really want to dig into the gory details, start with the source code[^] and follow the trail to see if you can find where the exception is thrown and caught. However, the System.Drawing classes use a lot of unmanaged code, so it's possible that you won't be able to see where the exception is thrown.
de-kekse 18-Nov-15 10:13am    
Thank you Richard.
I could have got myself the idea to debug the source. I think this will help to solve my issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900