Vb.net System.Drawing.dll引发了类型为';的未处理异常;System.ArgumentException';使用Graphics.GetHdc时

Vb.net System.Drawing.dll引发了类型为';的未处理异常;System.ArgumentException';使用Graphics.GetHdc时,vb.net,vb6,drawing,vb6-migration,gdi,Vb.net,Vb6,Drawing,Vb6 Migration,Gdi,我正在将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

我正在将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: 
我检查了各种网站和论坛,但我找不到解决这个问题的线索


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

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


分配太多的内存会使操作系统不安。它将拔掉插头,不会让你分配更多。异常有点混乱,GDI不提供错误信息。这是您可以在任务管理器的“流程”选项卡中看到的内容。使用视图>选择列并勾选GDI对象和用户对象。当你移动鼠标时,你应该看到第一只正在稳步爬升。该节目通常以10000个对象结束。

Hi,我添加了ReleaseHdc()来释放设备上下文,但任务管理器中的GDI对象仍然增加,我的代码在GetHdc崩溃。我已附上以下编辑代码供您参考。Dim hdc As IntPtr=g.GetHdc g_variable3D.retval=BitBlt(hdc,0,0,Picture1.Width,Picture1.Height,g_variable3D.jdc,0,0,TernaryRasterOperations.SRCCOPY)g.ReleaseHdc(hdc)’重要的是使用释放图形上下文结束的句柄,这就是我建议使用任务管理器的原因,像这样的漏洞几乎从来都不会只局限于一个bug。注释掉代码块以找到其他代码块。验证是否正确清理了g_variable3D.jdc。检查位图是否已被处置。等等。