加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net – PictureBox在Tab键按下时抛出“参数无效”ArgumentExc

发布时间:2020-12-17 07:25:26 所属栏目:百科 来源:网络整理
导读:我有一个表单,用户可以先扫描到位图.扫描完成后,加载位图,我有4个文本框然后启用.在每个文本框旁边,我有一个名为“从图像剪切”的按钮.当用户单击该按钮时,他们可以单击并拖动位图以使用MODI获取所选文本. 除了一个令人讨厌的bug之外,这种方法很完美:当我点
我有一个表单,用户可以先扫描到位图.扫描完成后,加载位图,我有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.PaintWithErrorHandling(PaintEventArgs e,Int16 layer)
>在System.Windows.Forms.Control.WmPaint(消息& m)
>在System.Windows.Forms.Control.WndProc(消息& m)
> at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
>在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
> at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)
>在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)
>在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
>在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
>在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String [] commandLine)
>在ORC_Testing.My.MyApplication.Main(String [] Args)in 17d14f5c-a337-4978-8281-53493378c1071.vb:第81行
> at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)
>在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)
>在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)
>在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.

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读