PictureBox throws an "Parameter invalid" ArgumentException when the tab key is pressed

I have a form where the user can first scan into a bitmap. When the scan is completed and the bitmap is loaded, I have 4 text fields, which are then included. Next to each text box there is a "Cut from image" button. When the user clicks the button, they can click and drag the bitmap to get the selected text using MODI.

This works fine, except for one annoying error: when I click the "Cut Image" button and drag the square, it gets the information in the text box. Then, if I click on the next text field, it will be very good, but if I use the tab key to leave the field, I get an "Parameter is invalid" ArgumentException , and it does not show any help for where the trouble code is. I can post to the form without any problems, but after scanning the bitmap it resets, like 9 out of 10 times, when I use the tab key.

I tried to override the tab key (for debugging only) 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 he still falls.

Any suggestions on what happened? Since I don’t know where to start debugging, I can’t say what code to show.

EDIT 1

Here is the stack trace for an ArgumentException that throws:

  • in System.Drawing.Image.get_Width ()
  • in System.Drawing.Image.get_Size ()
  • in System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode (PictureBoxSizeMode mode)
  • in System.Windows.Forms.PictureBox.OnPaint (PaintEventArgs pe)
  • in System.Windows.Forms.Control.PaintWithErrorHandling (PaintEventArgs e, Int16 layer)
  • in System.Windows.Forms.Control.WmPaint (Message & m)
  • in System.Windows.Forms.Control.WndProc (Message & m)
  • in System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Message & m)
  • in System.Windows.Forms.Control.ControlNativeWindow.WndProc (Message & m)
  • in System.Windows.Forms.NativeWindow.DebuggableCallback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  • in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW (MSG & msg)
  • in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop (IntPtr dwComponentID, reason Int32, Int32 pvLoopData)
  • in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (reason Int32, context ApplicationContext)
  • in 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)
  • in ORC_Testing.My.MyApplication.Main (String [] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb: line 81
  • in System.AppDomain._nExecuteAssembly (assembly RuntimeAssembly, String [] args)
  • in System.AppDomain.ExecuteAssembly (String assemblyFile, Evidence assemblySecurity, String [] args)
  • at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly ()
  • in System.Threading.ThreadHelper.ThreadStart_Context (object state)
  • in System.Threading.ExecutionContext.Run (ExecutionContext executeContext, ContextCallback callback, object state, Boolean ignoreSyncCtx)
  • in System.Threading.ExecutionContext.Run (ExecutionContext executeContext, ContextCallback callback, object state)
  • in System.Threading.ThreadHelper.ThreadStart ()

EDIT 2

This is how I scan / upload an 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. 
+4
source share
3 answers

Probably your problem is that at some point you are calling the Dispose method on one of your Image objects. When you call Image.Dispose , it removes the underlying image data from memory, so the Image object still exists, but is invalid because it no longer contains the actual image. When you set the PictureBox.Image property to a loaded Image object, the PictureBox control assumes that the Image object remains valid so that it can be reused later at any time when the control needs to be redrawn on the screen. For instance:

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

The PictureBox control will automatically delete the image for you when it is deleted, so you don’t have to worry about disposing of it yourself. The only time you have to delete images is when you do not give them any other objects for later use.

+9
source

Here is my solution, someone can use it, although the question is old.

 Dim myImage As Image = Image.FromFile("file path") PictureBox1.Image = myImage.clone // Use clone to give a new copy not a reference of image PictureBox1.Refresh() // This works myImage.Dispose() PictureBox1.Refresh() // This works also because we've a copy not reference 
+1
source

PictureBox1.Image = myImage.Clone This way you use a copy of the image, so it doesn't matter what happens to the original

0
source

All Articles