1

GetIconInfo我已经编写了一个应用程序,它使用的函数获取当前光标的图标信息user32.dll,它可以正常工作一段时间,但一段时间后它开始在ICONINFO.hbmMask(一些负值)中提供错误信息,并且在下一行我尝试从 中获取 Bitmap 对象Bitmap.HBitmap(bitmask),它会抛出异常:

A Generic error occured in GDI+.

从那里开始,它不断地给出这个异常,因为GetIconInfo总是返回负值(所有这些代码都在循环中工作)..

谁能告诉我这个问题是什么?以及如何避免下一次迭代异常?

这是代码

        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
4

2 回答 2

1

你的循环有多大?GDI+ 资源是操作系统资源,可用性有限。

您可以通过监视进程分配的 HANDLE来确定这是否是您的问题。如果 GDI+ 在某个句柄计数(HBITMAP 或 HICON)达到限制时开始抱怨,那么您知道您必须更智能地处理您的资源。您可以从使用任务管理器开始执行此操作,但可能希望切换到更复杂的软件,如Process Explorer

如果这是您的问题,那么您需要阅读IDisposable并确保Dispose在完成对象后调用它们(将不再呈现)。位图和图标以及大多数 GDI+ 对象都实现了 IDisposable。

此外,我不清楚,但您可能需要调用DeleteObject一些原始 GDI 对象本身(这一切都取决于您从哪里获得它们的句柄)。

于 2009-09-03T06:26:26.363 回答
0

查看此PInvoke示例,您是否正确删除了通过非托管代码拉入的对象?

于 2009-09-03T06:16:01.833 回答