I have written an application which is getting the Icon info of current cursor using GetIconInfo
function of user32.dll
, It works fine for some time, but after some time it starts providing wrong information in ICONINFO.hbmMask
(some negative value), and when on the next line I try to get Bitmap object from Bitmap.HBitmap(bitmask)
, it throws an exception:
A Generic error occured in GDI+.
From there onwords, it continuously gives this exception, as GetIconInfo
always return negative value (all this code is working in a loop)..
Can any one tell me what this problem is? and how to avoid the next iteration exception?
Here is the code
while (true)
{
//DLLimport User32.dll
PlatformInvokeUSER32.ICONINFO temp;
//Get the current cursor
IntPtr curInfo = GetCurrentCursor();
Cursor cur;
Icon ic;
if (curInfo != null && curInfo.ToInt32() != 0 && isOSelected)
{
cur = CheckForCusrors(curInfo);
try
{
//Dllimport User32.dll
//after some time the temp.hbmMask begin to get -ive vlaue from following function call
PlatformInvokeUSER32.GetIconInfo(curInfo, out temp);
if (temp.hbmMask != IntPtr.Zero)
{
//due to negative value of hbmMask the following function generates an exception
Bitmap bitmask = Bitmap.FromHbitmap(temp.hbmMask);
ic = Icon.FromHandle(curInfo);
Bitmap bmpCur = ic.ToBitmap();
}
}
catch (Exception ee)
{
//Exception message is
//A Generic error occured in GDI+
//and this loop begins to throw exception continuously
}
}
}// while ended