如何解决 - GDI +中发生了一般错误 [英] How To Solve - Generic error occurred in GDI+

查看:83
本文介绍了如何解决 - GDI +中发生了一般错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一段代码问题,我每隔500毫秒执行一次,根据CPU利用率在托盘上绘制自定义图标(例如任务管理器)



代码工作得很好,只是在经过一段时间的运行后它会抛出一个通用的GDI错误,我不知道如何解决。



有没有人知道如何处理这种情况吗?



Im having an issue with a piece of code that i execute every 500ms to draw a custom icon on the tray based on the CPU utilization (as task manager does for example)

The code works just fine except that after a short period of ran it throws a generic GDI error that i have no idea how to resolve.

Does anyone knows how to handle this situation?

Dim bCPU As New Bitmap(16, 16)
Dim gCPU As Graphics = Graphics.FromImage(bCPU)
'Draw Grid
For i As Integer = 1 To 16 Step 2
  gCPU.DrawLine(New Pen(Color.Green, 1), i, 0, i, 16)
  gCPU.DrawLine(New Pen(Color.Green, 1), 0, i, 16, i)
Next
'Draw Value
Dim lines As Integer = CInt(Math.Round((16 / 100) * perf_MeterCPU.LastValue, 0))
For i As Integer = 0 To lines Step 1
  gCPU.DrawLine(New Pen(Color.Lime, 1), 0, 16 - i, 16, 16 - i)
Next
'Draw Border
ControlPaint.DrawBorder3D(gCPU, 0, 0, 16, 16, Border3DStyle.SunkenInner)
TrayIconCPU.Icon = Icon.FromHandle(m_TrayBitmap.GetHicon)
bCPU.Dispose()
gCPU.Dispose()







例外bein抛出的是:



System.Runtime.InteropServices.ExternalException(0x80004005):GDI +中发生了一般性错误。




The exception being thrown is:

System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+.

推荐答案

首先想到的是你使用了错误的数字:你已经使用了16位像素16像素的位图,但是你正在绘制位图区域之外:0-16像素。



这不应该引起问题,额外应该在裁剪区域之外而不是被绘制。



您是否尝试删除gCPU声明与bCPU处理之间的所有代码,看它是否仍然存在?如果没有,请添加一些直到它...
The first thing that springs to mind is that you are using the wrong numbers: you have decalared a bitmap that is 16 pixels by 16 pixels, but you are drawing outside the bitmap area: pixels 0-16 inclusive.

This shouldn't cause a problem though, the extras should be outside the clipping region and not be drawn.

Have you tried removing all the code between the declaration of gCPU and the disposal of bCPU to see if it still does it? If it doesn't, add some back until it does...


您在循环的每次迭代中创建新的Pens,但从不处理它们。这是一个严重的资源泄漏。你应该为你想要使用的每种颜色创建一个SINGLE Pen对象,在整个绘图程序中使用它们,然后处理它们。



更好的是,在启动时你的应用程序,你可以全局创建笔,然后在你的应用程序存在时处理它们。
You're creating new Pens on every iteration of a loop, but never disposing them. This is a serious resource leak. You should be creating a SINGLE Pen object for each color you want to use, use those throughout your drawing routine, then Dispose them.

Better yet, on the startup of your app, you can get away with creating the Pens globally, then disposing of them when your app exists.


我意识到这是旧的,但我想我会指出真实的你得到这个错误的原因是因为InPtrs是不受管理的,这意味着你需要自己处理它们,因为.NET不会为你做这件事。

这一行:

I realize this is old, but I figured I'd point out that the real reason you are getting this error is because InPtrs are unmanaged, which means you need to dispose of them yourself as .NET will not do this for you.
This line:
TrayIconCPU.Icon = Icon.FromHandle(m_TrayBitmap.GetHicon)



需要更改为:


needs to be changed to:

IntPtr Hicon = img.GetHicon();
Icon newIcon = Icon.FromHandle(Hicon);
TrayIconCPU.Icon = newIcon;
DestroyIcon(newIcon.Handle);



为了调用DestroyIcon方法,你需要在课堂上使用它:


In order to call the DestroyIcon method you'll need this in your class:

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = CharSet.Auto)]
extern static bool DestroyIcon(IntPtr handle);



你需要添加使用for System.Runtime.InteropServices:


And you'll need to add using for System.Runtime.InteropServices:

using System.Runtime.InteropServices;





只是想帮助将来可能遇到的其他人。



Just wanted to help out others that may come across this in the future.


这篇关于如何解决 - GDI +中发生了一般错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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