中发生了一般错误

中发生了一般错误

本文介绍了如何解决 - 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+.

推荐答案



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 +中发生了一般错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:20