Exception thrown by System.Drawing

T

Tyler Foreman

Hello,

I had an earlier post regarding a strange exception I
(thought) I was getting in my windows form. I have since
been able to trace the problem to a picturebox control on
my form. I can reproduce the exception consistently, but
it seems to be timing dependent. Here is what happens:

I have a subform that I open, load an image from a file
into my PictureBox, take some user input, then I hide the
form. The last thing I do before I Hide the form is
release the Image's hold on the file by using Dispose().
This is where the problem occurs. The code is as follows:

'Release image resource
Try
PictureBox1.Image.Dispose()
Catch tobj As Exception
MsgBox(tobj.Message)
End Try

Try
Application.DoEvents()
Catch e
MsgBox(e.Message)
End Try

Me.Hide()

The exception is caught in the second Try. The message
says:
System.ArgumentException occured in
System.Windows.Forms.dll
Additional Information: Invalid Parameter Used.

Inspection of the exception shows that it is thrown by
System.Drawing. Inspection of the Stack Trace shows that
it is thrown from the "get_Width" method within the dll.
It appears to stem from when the PictureBox is redrawing
itself.

So here is my theory: It seems that there is some sort
of race condition between when I call Image.Dispose() and
when the Image is repainting itself that is causing this
problem. I haven't yet been able to verify this. Any
ideas? I would appreciate any help or input on this
problem.

Thanks alot. I have included the Stack Trace below.

Tyler Foreman

StackTrace" at System.Drawing.Image.get_Width()
at System.Drawing.Image.get_Size()
at System.Windows.Forms.PictureBox.get_ImageRectangle()
at System.Windows.Forms.PictureBox.OnPaint
(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling
(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage
(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc
(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback
(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW
(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms
..UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop
(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.ThreadContext.RunMessageLoopInner
(Int32 reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop
(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.DoEvents()
at MYAPP.Form2.SaveEvent_Click(Object eventSender,
EventArgs eventArgs)
 
F

Fergus Cooney

Hi Tyl;er,

PictureBox1.Image.Dispose() will dispose of the underlying resource used
by the Image, ie the internal BitMap. PictureBox1.Image is still there,
however, so when PictureBox1_Paint occurs, it says "Ah here's an Image to
draw" but then there's no BitMap inside. :-(

You need:
PictureBox1.Image.Dispose()
PictureBox1.Image = Nothing

Then System.Windows.Forms.PictureBox.OnPaint won't bother trying to call
get_ImageRectangle().

Regards,
Fergus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top