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" ArgumentException
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.
EDIT 1
Here is the stack trace for the ArgumentException
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()
EDIT 2
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.