System.Drawing.dll 在使用 Graphics.GetHdc 时引发了类型为 'System.ArgumentException' 的未处理异常
System.Drawing.dll threw an unhandled exception of type 'System.ArgumentException' when Graphics.GetHdc is used
我正在将代码从 VB6 翻译成 VB .NET。
这里是 VB6 代码:
retval = BitBlt(Picture1.hdc, 0, 0, Picture1.Width, Picture1.Height, jdc, 0, 0, SRCCOPY)
等效的 VB .NET 代码是:
Dim gr As Graphics = Picture1.CreateGraphics()
Dim hdc As IntPtr = gr.GetHdc()
g_variable3D.retval = BitBlt(hdc, 0, 0, Picture1.Width, Picture1.Height, g_variable3D.jdc,
0, 0, TernaryRasterOperations.SRCCOPY)
当我 运行 代码时,我收到一条错误消息:
Dim hdc As IntPtr = gr.GetHdc()
错误消息说:
An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll. Additional information: Parameter is not valid.
这是堆栈跟踪:
StackTrace:
at System.Drawing.Graphics.GetHdc()
at frm3D.vb:line 313
at frm3D.Picture1_MouseMove(Object sender, MouseEventArgs e) in frm3D.vb:line 1176
at System.Windows.Forms.Control.OnMouseMove(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseMove(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 My.MyApplication.Main(String[] Args) in :line 81
InnerException:
我查看了各种网站和论坛,但找不到解决此问题的线索。
如何处理这个异常?哪个参数在这里无效?
是的,这最终会发生。在 MouseMove 事件处理程序中不会花费很长时间。由于 Graphics.GetHdc() 的文档没有忘记指出,您 必须 调用 ReleaseHdc() 再次释放设备上下文。
分配太多会使操作系统不正常。它会拔掉插头,不会让你分配更多。异常有点混乱,GDI不提供错误信息。这是您可以在任务管理器的“进程”选项卡中看到的内容。使用 View > Select 列并勾选 GDI Objects 和 USER Objects。当您移动鼠标时,您应该看到第一个稳步攀升。该节目通常以 10,000 个对象结束。
我正在将代码从 VB6 翻译成 VB .NET。
这里是 VB6 代码:
retval = BitBlt(Picture1.hdc, 0, 0, Picture1.Width, Picture1.Height, jdc, 0, 0, SRCCOPY)
等效的 VB .NET 代码是:
Dim gr As Graphics = Picture1.CreateGraphics()
Dim hdc As IntPtr = gr.GetHdc()
g_variable3D.retval = BitBlt(hdc, 0, 0, Picture1.Width, Picture1.Height, g_variable3D.jdc,
0, 0, TernaryRasterOperations.SRCCOPY)
当我 运行 代码时,我收到一条错误消息:
Dim hdc As IntPtr = gr.GetHdc()
错误消息说:
An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll. Additional information: Parameter is not valid.
这是堆栈跟踪:
StackTrace:
at System.Drawing.Graphics.GetHdc()
at frm3D.vb:line 313
at frm3D.Picture1_MouseMove(Object sender, MouseEventArgs e) in frm3D.vb:line 1176
at System.Windows.Forms.Control.OnMouseMove(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseMove(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 My.MyApplication.Main(String[] Args) in :line 81
InnerException:
我查看了各种网站和论坛,但找不到解决此问题的线索。
如何处理这个异常?哪个参数在这里无效?
是的,这最终会发生。在 MouseMove 事件处理程序中不会花费很长时间。由于 Graphics.GetHdc() 的文档没有忘记指出,您 必须 调用 ReleaseHdc() 再次释放设备上下文。
分配太多会使操作系统不正常。它会拔掉插头,不会让你分配更多。异常有点混乱,GDI不提供错误信息。这是您可以在任务管理器的“进程”选项卡中看到的内容。使用 View > Select 列并勾选 GDI Objects 和 USER Objects。当您移动鼠标时,您应该看到第一个稳步攀升。该节目通常以 10,000 个对象结束。