调用Bitmap.GetHicon时发生在GDI一般性错误+ [英] A Generic Error occurred in GDI+ when calling Bitmap.GetHicon

查看:1512
本文介绍了调用Bitmap.GetHicon时发生在GDI一般性错误+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我收到一般性错误发生在GDI +异常?

Why I'm getting "A Generic Error occurred in GDI+" Exception ?

IntPtr hicon = tempBitmap.GetHicon();             
Icon bitmapIcon = Icon.FromHandle(hicon);            
return bitmapIcon;

在我的应用程序已经运行了30分钟以上时发生错误。 (我转换 System.Drawing.Bitmap System.Drawing.Icon 每秒)

The error occurred when my application has been running for more than 30 minutes. (I am converting System.Drawing.Bitmap to System.Drawing.Icon every second)

推荐答案

这是由一个句柄泄漏引起的。可以诊断出的TaskMgr.exe,进程选项卡中的泄漏。查看+选择列和蜱把手,GDI对象和用户对象。你的程序运行时观察这些列。如果我的猜测是正确的,你会看到GDI为您的流程稳步攀升对象的价值。当它达到10,000时那么表演结束时,Windows拒绝让你漏更多的对象。

That's caused by a handle leak. You can diagnose the leak with TaskMgr.exe, Processes tab. View + Select Columns and tick Handles, GDI Objects and USER Objects. Observe these columns while your program is running. If my guess is right, you'll see the GDI Objects value for your process steadily climbing. When it reaches 10,000 then the show is over, Windows refuses to allow you to leak more objects.

有关Icon.FromHandle说明部分说:

The Remarks section for Icon.FromHandle says:

在使用这种方法,你必须使用DestroyIcon方法Win32 API中,以确保资源被释放处置所产生的图标。

When using this method you must dispose of the resulting icon using the DestroyIcon method in the Win32 API to ensure the resources are released.

这是个好建议,但通常相当痛苦的事情。你可以找到一个黑客以强制Icon对象给自己的手感,并自动释放,在这个答案。相关的代码是私有调用构造函数图标部分之后。

That's good advice but usually pretty painful to do. You can find a hack to force the Icon object to own the handle, and automatically release it, in this answer. Relevant code is after the "Invoke private Icon constructor" section.

这篇关于调用Bitmap.GetHicon时发生在GDI一般性错误+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆