gpt4 book ai didi

vb.net - 按 T​​ab 键时 PictureBox 抛出 "Parameter is not valid"ArgumentException

转载 作者:行者123 更新时间:2023-12-02 05:19:59 24 4
gpt4 key购买 nike

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

除了一个恼人的错误之外,这一切都很完美:当我单击“从图像剪切”按钮并拖动一个正方形时,它可以很好地将信息获取到文本框。然后,如果我单击下一个文本框,效果会很好,但是如果我使用 Tab 键离开该字段,则会收到“参数无效”ArgumentException 并且它不会显示有关代码中发生崩溃的位置的任何帮助。我可以在表单中进行 Tab 切换,没有任何问题,但是一旦扫描了位图,当我使用 Tab 键时,它就会崩溃,十分之九。

我尝试使用以下命令覆盖 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.PaintWithErrorHandling(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.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
  • 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.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(String[] commandLine)
  • 位于 17d14f5c-a337-4978-8281-53493378c1071.vb 中的 ORC_Testing.My.MyApplication.Main(String[] Args):第 81 行
  • 位于 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,String[] args)
  • 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,String[] args)
  • 位于 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  • 位于 System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
  • 在 System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext、ContextCallback 回调、对象状态、 bool 值ignoreSyncCtx)
  • at System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,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.

最佳答案

您的问题可能是,在某个时刻,您正在对其中一个 Image 对象调用 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 控件在处理图像时会自动为您处理图像,因此您无需担心自己处理图像。您应该处理图像的唯一时间是当您不将它们提供给任何其他对象以供以后使用时。

关于vb.net - 按 T​​ab 键时 PictureBox 抛出 "Parameter is not valid"ArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13912367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com