由于C#精简框架集绘图函数不支持圆角矩形,所以引用了相关的API,
WinCE平台下C#引用API(GDI)中注意内存泄漏.net
。 [DllImport("\\ windows \\coredll.dll ", EntryPoint = "RoundRect")] private static extern int CeRoundRect(IntPtr hdc, int X1, int Y1, int X2, int Y2, int X3, int Y3); 这是有内存由于C#精简框架集绘图函数不支持圆角矩形,所以引用了相关的API。
<PRE></p><p> [DllImport("\\<strong>windows</STRONG>\\coredll.dll", EntryPoint = "RoundRect")]</p><p> private static extern int CeRoundRect(IntPtr hdc,</p><p> int X1, int Y1, int X2, int Y2, int X3, int Y3);</p></PRE>
这是有内存泄漏的源码:
<PRE></p><p> public static int RoundRect(Graphics e, Pen pen, SolidBrush brush,</p><p> int X1, int Y1, int X2, int Y2, int X3, int Y3)</p><p> {</p><p> IntPtr hpen;</p><p> IntPtr hbrush;</p><p> if(pen!=null)</p><p> {</p><p> hpen = CreatePen((DashStyle.Solid == pen.DashStyle) ? 0 : 1,</p><p> (int)pen.Width, SetRGB(pen.Color.R, pen.Color.G, pen.Color.B)); //创建GDI画笔</p><p> }</p><p> else</p><p> {</p><p> hpen = GetStockObject(8); //空画笔</p><p> }</p><p> if (brush!= null)</p><p> {</p><p> hbrush = CreateSolidBrush(SetRGB(brush.Color.R, brush.Color.G, brush.Color.B));</p><p> //brush.Color.ToArgb());</p><p> }</p><p> else</p><p> {</p><p> hbrush = GetStockObject(5);</p><p> }</p><p> //pen.Dispose();</p><p> //brush.Dispose();</p><p> IntPtr hdc = e.GetHdc();</p><p> //---------------------</p><p> SelectObject(hdc, hbrush);</p><p> SelectObject(hdc, hpen);</p><p> int intRet=RoundRect(hdc, X1, Y1, X2,Y2, X3, Y3);</p><p> DeleteObject(hbrush);</p><p> DeleteObject(hpen);</p><p> //---------------------</p><p> e.ReleaseHdc(hdc);</p><p> return intRet;</p><p> }</p></PRE>
这是没有问题的源码:
<PRE></p><p> public static int RoundRect(Graphics e,</p><p> Pen pen, SolidBrush brush, int X1, int Y1, int X2,</p><p> int Y2, int X3, int Y3)</p><p> {</p><p> IntPtr hpen,old_pen;</p><p> IntPtr hbrush, old_brush;</p><p> if(pen!=null)</p><p> {</p><p> hpen = CreatePen((DashStyle.Solid == pen.DashStyle) ? 0 : 1,</p><p> (int)pen.Width, SetRGB(pen.Color.R, pen.Color.G, pen.Color.B));</p><p> //创建GDI画笔</p><p> }</p><p> else</p><p> {</p><p> hpen = GetStockObject(8); //空画笔</p><p> }</p><p> if (brush!= null)</p><p> {</p><p> hbrush = CreateSolidBrush(SetRGB(brush.Color.R,</p><p> brush.Color.G, brush.Color.B)); //brush.Color.ToArgb());</p><p> }</p><p> else</p><p> {</p><p> hbrush = GetStockObject(5);</p><p> }</p><p> //pen.Dispose();</p><p> //brush.Dispose();</p><p> IntPtr hdc = e.GetHdc();</p><p> //---------------------</p><p> old_brush=SelectObject(hdc, hbrush);</p><p> old_pen=SelectObject(hdc, hpen);</p><p> int intRet=RoundRect(hdc, X1, Y1, X2,Y2, X3, Y3);</p><p> SelectObject(hdc, old_brush);</p><p> SelectObject(hdc, old_pen);</p><p> DeleteObject(hbrush);</p><p> DeleteObject(hpen);</p><p> //---------------------</p><p> e.ReleaseHdc(hdc);</p><p> return intRet;</p><p> }</p></PRE>
看出代码的区别来了没有?泄漏的原因其实很简单,就是没有重新选入旧的画笔画刷,
电脑资料
《WinCE平台下C#引用API(GDI)中注意内存泄漏.net》(https://www.unjs.com)。同样的程序(当然PC端的API库是GDI32)在上位机WindowXP平台上没有什么问题(测试大约3天以上),而在WinCE平台确非常明显,大约1~3个小时(视圆角矩形绘图的多寡而定),该程序就会内存耗尽而死。来源链接:http://blog.csdn.net/yefanqiu/archive/2006/12/05/1430411.aspx
(责任编辑 火凤凰 sunsj@51cto.com TEL:(010)68476636-8007)
原文转自:http://www.ltesting.net