473,394 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Exception thrown by System.Drawing

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
2 5934
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Craig Vermeer | last post by:
Hi All, I noticed an interesting issue the other day, and thought someone here might be able to shed some light on things. I have a situation where I'm using asynchronous delegates, and an...
3
by: PB | last post by:
As part of an ASP.NET WEb Application I have a routine (relevant portion is below) that lets users rotate a photo (jpg or gif). The routine works just fine if it is run once. If run a second time...
3
by: Minh | last post by:
I create a C# Project using Windows Application template, I add an imagelist to Form, and add icon to imagelist1. After that, I run Project, but an Exception has occured. I can fix it even...
3
by: will | last post by:
Hi all. I've got an question about how to catch an exception. In Page_Load, I place a DataGrid, dg1, into edit mode. This will call the method called GenericGridEvent. GenericGridEvent will call...
4
by: Brian Henry | last post by:
I get the following exception only on one system (dev system) and not on any others... what could cause this?! I can access the SMTP server just fine in E-Mail applications on the machine... ...
7
by: ZorpiedoMan | last post by:
Well, I'm still wondering around in the "land of the lost exception"... It seems that controls that are bound to a class and the class throws an error in the SET method of the bound member, the...
16
by: Chuck Cobb | last post by:
I'm implementing a centralized exception handling routine using the Enterprise Library Exception Management Application Block. I trap all unhandled exceptions to one place using the following...
2
by: Eugene | last post by:
Hi, I have a problem when loading one of my dll. I have a main class that would load a winform in another dll using reflection. When I load it using Assembly.CreateInstance(DLLName.DLLFormName), an...
1
by: neoupadhyay | last post by:
External component has thrown an exception. C:\Program Files\Microsoft Visual Studio 8\Common7\IDE> "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe" /t:library /utf8output...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.