如何解决winforms错误"GDI +中发生了一般错误."?

M_I*_*ees 4 forms windows c#-2.0

我在C#.net win forms应用程序中遇到以下异常.

GDI +中发生了一般错误.
在System.Drawing.Graphics.CheckErrorStatus(Int32 status)
的System.Drawing.Graphics.DrawRectangle(Pen pen,Int32 x,Int32 y,Int32 width,Int32 height)
at WeifenLuo.WinFormsUI.Docking.DockWindow.OnPaint(PaintEventArgs e)
在System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,Int16 layer,Boolean disposeEventArgs)
处于System.Windows.Forms.Control.WmPaint(Message&m)
的System.Windows.Forms.Control.WndProc(Message&m)
at System
System.Windows.Forms上System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)
的System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)
中的.Windows.Forms.ScrollableControl.WndProc(Message&m).NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

最令人困惑的一点是,当应用程序加载表单(包含一些丰富的图形,WPF工作等)时,很少会出现表单成功加载的大约90%的时间.但很少有时候它会被复制,而且只在某些机器上运行,在某些其他机器上,这种形式在100%的时间内都可以工作,并且永远不会遇到这种情况.

我不知道为什么会发生这种异常,因为它也没有显示导致异常的确切堆栈跟踪.如果有人知道如何处理它,请建议.

Han*_*ant 9

您的代码可能会严重泄漏GDI资源.看看Taskmgr.exe,进程选项卡.查看+选择列并勾选句柄,USER对象和GDI对象.运行程序并观察过程的显示值.GDI对象不断攀升的价值会带来麻烦,当节目达到10,000时,节目就结束了.

究竟什么可能导致泄漏并不那么容易诊断.虽然您可以在调试器中单步执行代码并密切关注taskmgr编号.经典的错误是在Paint事件处理程序中创建笔和画笔而不是处理它们.没有垃圾收集器经常运行足以清理.使用using语句进行修复.