使用Graphics.GetHdc时,System.Drawing.dll抛出了类型'System.ArgumentException'的未处理异常

时间:2016-01-19 08:59:44

标签: vb.net vb6 drawing vb6-migration hdc

我正在将代码从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()

错误消息显示:

  

System.Drawing.dll中发生了未处理的“System.ArgumentException”类型异常。附加信息:参数无效。

这是堆栈跟踪:

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: 

我检查了各种网站和论坛,但我找不到解决这个问题的线索。

如何处理此异常?什么参数在这里无效?

1 个答案:

答案 0 :(得分:3)

是的,这最终会发生。在MouseMove事件处理程序中花费很长时间。由于Graphics.GetHdc()的文档无法指出,您必须调用ReleaseHdc()以再次释放设备上下文。

分配太多的操作系统会让操作系统感到不安。它会拉动插头并且不会让你分配更多。例外有点令人困惑,GDI不提供错误信息。您可以在“任务管理器”,“进程”选项卡中看到这一点。使用视图>选择列并勾选GDI对象和用户对象。当您移动鼠标时,您应该看到第一个稳定攀爬。该节目通常以10,000件物品结束。