By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,556 Members | 1,479 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,556 IT Pros & Developers. It's quick & easy.

Exception thrown by System.Drawing

P: n/a
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.PaintWithErrorHandlin g
(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.DebuggableCallba ck
(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW
(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windo ws.Forms
..UnsafeNativeMethods+IMsoComponentManager.FPushMe ssageLoop
(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.ThreadContext.RunMessageLoopI nner
(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)
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Nov 20 '05 #2

P: n/a
Thanks Fergus! That did the trick!

Cheers,
Tyler Foreman
-----Original Message-----
Hi Tyl;er,

PictureBox1.Image.Dispose() will dispose of the underlying resource usedby the Image, ie the internal BitMap. PictureBox1.Image is still there,however, so when PictureBox1_Paint occurs, it says "Ah here's an Image todraw" 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 callget_ImageRectangle().

Regards,
Fergus
.

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.