Vb.net PictureBox抛出;参数无效";tab键按下时出现异常

Vb.net PictureBox抛出;参数无效";tab键按下时出现异常,vb.net,winforms,bitmap,keypress,modi,Vb.net,Winforms,Bitmap,Keypress,Modi,我有一个表单,用户可以首先扫描到位图。当扫描完成并加载位图时,我有4个文本框,然后启用。在每个文本框旁边,我都有一个名为“从图像剪切”的按钮。当用户单击按钮时,他们可以在位图中单击并拖动以使用MODI获取所选文本 除了一个恼人的bug外,这一切都很完美:当我点击一个“从图像剪切”按钮并拖动一个正方形时,它会很好地将信息放到文本框中。然后,如果我单击下一个文本框,它运行得很好,但是如果我使用tab键离开该字段,我会得到一个“参数无效”ArgumentException,并且它不会显示代码中发生崩溃

我有一个表单,用户可以首先扫描到位图。当扫描完成并加载位图时,我有4个文本框,然后启用。在每个文本框旁边,我都有一个名为“从图像剪切”的按钮。当用户单击按钮时,他们可以在位图中单击并拖动以使用MODI获取所选文本

除了一个恼人的bug外,这一切都很完美:当我点击一个“从图像剪切”按钮并拖动一个正方形时,它会很好地将信息放到文本框中。然后,如果我单击下一个文本框,它运行得很好,但是如果我使用tab键离开该字段,我会得到一个“参数无效”
ArgumentException
,并且它不会显示代码中发生崩溃的位置的任何帮助。我可以在表单中随意切换,一点问题都没有,但是一旦扫描位图,当我使用tab键时,它会崩溃10次中的9次

我尝试使用以下命令覆盖tab键(仅用于调试):

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
…但它仍然崩溃

有什么不对的建议吗?因为我不知道从哪里开始调试,所以我不知道要显示什么代码

编辑1

以下是引发的
ArgumentException
的堆栈跟踪:

  • 在System.Drawing.Image.get_Width()处
  • 在System.Drawing.Image.get_Size()中
  • 位于System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode模式)
  • 位于System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
  • 在System.Windows.Forms.Control.PaintEventArgs处理(PaintEventArgs e,Int16层)
  • 在System.Windows.Forms.Control.WmPaint(Message&m)中
  • 位于System.Windows.Forms.Control.WndProc(Message&m)
  • 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)中
  • 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中
  • 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam)
  • 在System.Windows.Forms.UnsafentiveMethods.DispatchMessageW(MSG&MSG)中
  • 位于System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafentiveMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID、Int32 reason、Int32 pvLoopData)
  • 位于System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)
  • 位于System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)
  • 在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()上
  • 在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()上
  • 在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(字符串[]命令行)
  • 在17d14f5c-a337-4978-8281-53493378c1071中的ORC_Testing.My.MyApplication.Main(字符串[]Args)处。vb:第81行
  • 位于System.AppDomain.\u nExecuteAssembly(RuntimeAssembly程序集,字符串[]args)
  • 位于System.AppDomain.ExecuteAssembly(字符串汇编文件、证据汇编安全性、字符串[]args)
  • 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上
  • 位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态)
  • 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔ignoreSyncCtx)
  • 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
  • 位于System.Threading.ThreadHelper.ThreadStart()处
编辑2

以下是我扫描/加载图像的方式:

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.

您的问题可能是,在某个时刻,您正在对一个
图像
对象调用
Dispose
方法。调用
Image.Dispose
时,它会从内存中删除基础图像数据,因此
Image
对象仍然存在,但无效,因为它不再包含实际图像。当您将
PictureBox.Image
属性设置为加载的
Image
对象时,
PictureBox
控件假定
Image
对象将保持有效,以便在以后控件需要将自身重新绘制到屏幕上时可以重新使用它。例如:

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

PictureBox
控件将在图像被处置时自动为您处置图像,因此您无需担心自己处置图像。处理图像的唯一时间是不将图像提供给任何其他对象供以后使用。

PictureBox1.Image=myImage.Clone
通过这种方式,您使用的是图像的副本,因此无论原始图像会发生什么情况,这里是我的解决方案,有人可以使用它,即使这个问题很老

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 

您有来自异常的调用堆栈吗?你知道代码中哪一行出错了吗?你能展示代码的这一部分吗?是的,但因为我不太喜欢VS,我不知道从哪里开始。消息:“参数无效”。资料来源:“系统图”。你想要“StackTrace”?哦,不。我真的看不出它在代码中哪里失败了。你是在用调试器运行应用程序吗?如果是这样,应该有一种方法来显示异常的详细信息和堆栈跟踪。至少,堆栈跟踪将显示发生错误的方法。请尝试删除任何
图像
对象上调用的
Dispose
位置,然后查看错误是否消失。非常感谢!很好的解释!仍然很有趣的是,使用鼠标完成所有这些工作,创建和处置实例,然后当我使用tab在字段之间移动时,它崩溃了。但现在这两种方法都有效了是的,这有点奇怪,但并非完全出乎意料。在使用键盘时,无论出于何种原因,Windows都认为需要重新绘制PictureBox,但在使用