المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Cursor change in WinForms - A generic error occurred in GDI +



C# Programming
01-27-2010, 05:30 AM
For windows I change the color of the cursor depending on what background color is now.

Code below. Unfortunately, from time to time (too often) gets an exception:


System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI +
At System.Drawing.Bitmap.GetHicon ()

and the end of the program.

A year ago I wrote an earlier version, a mechanism to change the cursor the same, the same code, but then I used VisualStudio 2005, at 32bit Vista. And never once I had such an exception.

vs2008 and now the win7 x64.

I don't checked yet whether to build VS2005 help ...

However, it may be any known explanation and a solution to this problem.



I use IntPtr as a handle to the icon, and 64 bit systems, it is 64 bit and 32-bit Win 32 .. but not further Jazee what the problem ...

Shows the error both on and win764 win xp32 ...



private void changeCursor(int type)
{
int _cursorSizePx = Convert.ToInt32(((200.0 * _cursorSize) / _calibration));//px

Bitmap b = new Bitmap(_cursorSizePx + 1, _cursorSizePx + 1);
Graphics g = Graphics.FromImage(b);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//draw something
switch (type)
{
case 0:

g.FillEllipse(new SolidBrush(_cursorColor), 0, 0, _cursorSizePx, _cursorSizePx);
break;
case 1:
g.DrawEllipse(new Pen(new SolidBrush(_figureColor), 3), 1, 1, _cursorSizePx - 3, _cursorSizePx - 3);
g.FillEllipse(new SolidBrush(_sqreenColor), 1, 1, _cursorSizePx - 3, _cursorSizePx - 3);
break;
}
IntPtr ptr = b.GetHicon(); //<span class="code-comment">