加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows – 如何解决winforms错误“GDI中发生了一般错误. “?

发布时间:2020-12-14 04:03:01 所属栏目:Windows 来源:网络整理
导读:我在C#.net win forms应用程序中遇到以下异常. A generic error occurred in GDI+. at System.Drawing.Graphics.CheckErrorStatus(Int32 status) at System.Drawing.Graphics.DrawRectangle(Pen pen,Int32 x,Int32 y,Int32 width,Int32 height) at WeifenLuo
我在C#.net win forms应用程序中遇到以下异常.

A generic error occurred in GDI+.
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.DrawRectangle(Pen pen,Int32 x,Int32 y,Int32 width,Int32 height)
at WeifenLuo.WinFormsUI.Docking.DockWindow.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,Int16 layer,Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.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.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

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

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

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读