图片框抛出"参数无效"在tab键preSS的ArgumentException [英] PictureBox throws "Parameter is not valid" ArgumentException upon tab keypress

查看:199
本文介绍了图片框抛出"参数无效"在tab键preSS的ArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,用户可以先扫描到一个位图。当扫描完成后,和位图被加载,我有4个文本框,然后被启用。下一步,每个文本框,我有一个名为按钮,从图像剪切。当用户点击该按钮,就可以单击并拖动位图使用MODI得到选定的文本。

这工作,除了一个恼人的错误完美:当我点击了剪切从图像按钮,拖动一个正方形,它很好地获取信息的文本框中。然后,如果我点击到下一个文本框,它会非常好,但如果我使用tab键离开现场,我得到一个参数无效的ArgumentException 并没有显示其中的code坠毁是由任何帮助。我可以在标签与完全没有问题的形式左右,但一旦位图被扫描,它崩溃就像9个10倍,当我使用Tab键。

我用这个试图重写tab键(只用于调试):

 受保护的覆盖功能ProcessTabKey(BYVAL向前布尔)为布尔
    MSGBOX(TAB目前已禁用!)
    返回False尝试也是如此,以防万一
结束功能

...但它仍然崩溃。

不好的事有什么建议?因为我不知道从哪里开始调试我不能告诉code显现。

修改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(消息和M)

  • 在System.Windows.Forms.Control.WndProc(消息和M)

  • 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息和M)

  • 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和M)

  • 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND,味精的Int32,IntPtr的WPARAM,LPARAM的IntPtr)

  • 在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&安培;味精)

  • 在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,的Int32原因,的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(字符串[]命令行)

  • 在ORC_Testing.My.MyApplication.Main(字串[] args)在17d14f5c-a337-4978-8281-53493378c1071.vb:行81

  • 在System.AppDomain._nExecuteAssembly(RuntimeAssembly组装,字串[] args)

  • 在System.AppDomain.ExecuteAssembly(字符串assemblyFile,证据assemblySecurity,字串[] args)

  • 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

  • 在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)

  • 在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态,布尔ignoreSyncCtx)

  • 在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态)

  • 在System.Threading.ThreadHelper.ThreadStart()

编辑2

下面是如何我扫描/加载图像:

 昏暗的文件名作为集合
文件名= TwainHandler.ScanImages(C:\\扫描\\,TIF)
昏暗ScannedFile作为图像= Image.FromFile(文件名(1))
PictureBox1.Image = ScannedFile
PictureBox1.Width = ScannedFile.Width
等。


解决方案

您的问题可能是,在某些时候,您呼叫的的Dispose 方法上的一个你图片的对象。当你调用 Image.Dispose ,它从内存中删除底层图像数据,因此图片对象仍然存在,但是无效的,因为它不再包含实际图像。当你在 PictureBox.Image 属性设置为加载图片对象时,<​​code>图片框控制假定图片对象将仍然有效,以便它可以在以后重新使用它的任何时间控制需要重新绘制自己到屏幕。例如:

 昏暗MYIMAGE作为图像= Image.FromFile(文件路径)
PictureBox1.Image = MYIMAGE
PictureBox1.Refresh()这工作
myImage.Dispose()
PictureBox1.Refresh()这是因为它试图访问被处置Image对象抛出一个异常

它被设置时,图片框控件会自动配置为你的形象,所以你不必担心自己处理吧。你应该处理图像的唯一情况是,当你不给他们以供以后使用的任何其他对象。

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.

解决方案

Your problem is likely that, at some point, you are calling the Dispose method on one of your Image objects. When you call Image.Dispose, it deletes the underlying image data from memory, so the Image object still exists, but is invalid because it no longer contains an actual image. When you set the PictureBox.Image property to a loaded Image object, the PictureBox control assumes that the Image object will remain valid so that it can reuse it later any time the control needs to repaint itself to 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 dispose of the image for you when it is disposed, so you don't need to worry about disposing it yourself. The only time you should be disposing your images is when you are not giving them to any other objects for later use.

这篇关于图片框抛出&QUOT;参数无效&QUOT;在tab键preSS的ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆