gubbfett gubbfett - 2 years ago 88 Question

PictureBox throws "Parameter is not valid" ArgumentException upon tab keypress

I have a form where the user can first scan to a bitmap. When scan is done, and the bitmap is loaded, I have 4 text boxes that are then enabled. Next to each text box, I have a button called "Cut from image". When the user clicks the button, they can click and drag in the bitmap to get the selected text using MODI.

This works perfect except for one annoying bug: When I click a "Cut from image" button and drag a square, it gets the information nicely to the text box. Then, if i click to the next text box, it goes very well, but if I use the tab key to leave the field, I get a "Parameter is not valid"

and it does not show any help for where in the code the crash is made. I can tab around in the form with no problems at all, but once the bitmap is scanned, it crashes like 9 out of 10 times when I use the tab key.

I tried to override the tab key (just for debugging) using this:

Protected Overrides Function ProcessTabKey(ByVal forward As Boolean) As Boolean
MsgBox("TAB is currently disabled!")
Return False 'Tried True as well, just in case
End Function

...but it still crashes.

Any suggestions about what's wrong? Since I don't know where to begin debugging I can't tell what code to show.


Here is the stack trace for the
that gets thrown:

  • at System.Drawing.Image.get_Width()

  • at System.Drawing.Image.get_Size()

  • at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)

  • at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)

  • at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)

  • at System.Windows.Forms.Control.WmPaint(Message& m)

  • at System.Windows.Forms.Control.WndProc(Message& m)

  • at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

  • at System.Windows.Forms.Control.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.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)

  • at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

  • at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

  • at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()

  • at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()

  • at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)

  • at ORC_Testing.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81

  • at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)

  • at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

  • at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

  • at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

  • at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)

  • at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

  • at System.Threading.ThreadHelper.ThreadStart()


Here is how I'm scanning/loading the image:

Dim filename As Collection
filename = TwainHandler.ScanImages("c:\scan\", "tif")
Dim ScannedFile As Image = Image.FromFile(filename(1))
PictureBox1.Image = ScannedFile
PictureBox1.Width = ScannedFile.Width
' etc.

Answer Source

Your problem is likely that, at some point, you are calling the Dispose method on one of your Image objects. When you call Image.Dispose, it deletes the underlying image data from memory, so the Image object still exists, but is invalid because it no longer contains an actual image. When you set the PictureBox.Image property to a loaded Image object, the PictureBox control assumes that the Image object will remain valid so that it can reuse it later any time the control needs to repaint itself to the screen. For instance:

Dim myImage As Image = Image.FromFile("file path")
PictureBox1.Image = myImage
PictureBox1.Refresh() ' This works
PictureBox1.Refresh() ' This throws an exception because it tries to access the disposed Image object

The PictureBox control will automatically dispose of the image for you when it is disposed, so you don't need to worry about disposing it yourself. The only time you should be disposing your images is when you are not giving them to any other objects for later use.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download