How do you trap errors from System.Drawing.dll?

When I execute the following code, I get the error - An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.Drawing.dll. In this example it occurs because the wmf file is very large and PictureBox1 size mode is Auto.

I know I can prevent this error, but that's not the point. The point is I can't trap it. The error never gets caught.

Can anyone please enlighten me as to why this is possible and what if anything can be done about it I thought exception error handling was supposed to prevent this type of thing from happening

On a separate note, it would be nice to know (or be able to calculate) the size limitation of the PictureBox control. My guess is it's hardware dependent and therefore not documented. Any help is appreciated.

Thank you.

Try

PictureBox1.Image = Image.FromFile("ImageTest.wmf")

Catch ex As SystemException

MsgBox("Trapped Error Win32Exception " + ex.Message)

Catch ex As ApplicationException

MsgBox("Trapped Error ApplicationException " + ex.Message)

Catch ex As Exception

MsgBox("Trapped Error Exception " + ex.Message)

End Try



Answer this question

How do you trap errors from System.Drawing.dll?

  • IamHuM

    Thank you for the responses. Ken, assigning the value to the image first did not appear to help. nobugz, it was a good idea to try the paint event but that didn't work either. McWhirter, thanks for the tip.

    I also tried trapping the error with "On Error Goto" but that didn't seem to work either.

    I think there two issues here. That the System.Drawing.dll does not always properly throw or raise errors, and when this happens (in this example or others) that there is apparently nothing that can be done about it.

    This may be a unique case since it most likely is caused by a memory overrun, but you would hope a MS provided dll would behave better.

    Hopefully someone from Microsoft is monitoring this forum and can enlighten us on this.


  • ccondit

    Perhaps the PictureBox control is faulting on the Paint event. It should display the "ErrorImage" picture but doesn't do this if the exception is considered "critical". Try trapping the exception with code like this:

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    Try
    MyBase.OnPaint(e)
    Catch ex As Exception
    MsgBox(ex.ToString)
    PictureBox1.Image = PictureBox1.ErrorImage
    End Try
    End Sub



  • stake75

    From previous experience, its often better to use ex.tostring, rather than ex.message. Sometimes the info provided by the tostring option is far more useful.
  • Kapalic

    Maybe this will work better

    Try
    Dim img as image
    img=Image.FromFile("ImageText.wmf")
    picturebox1.image=img
    etc....................


  • How do you trap errors from System.Drawing.dll?