GetIconInfo 函数无法正常工作

发布于 2024-08-03 06:28:17 字数 1710 浏览 6 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏闹i 2024-08-10 06:28:17

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

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

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

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

How large is your loop? GDI+ resources are OS resources and are limited in availability.

You can find out if this is your problem by monitoring the HANDLEs allocated by your process. If GDI+ starts to complain when a certain handle count (HBITMAP or HICON) reaches a limit, then you know you have to deal with your resources more intelligently. You can start by using Task Manager to do this but might want to switch to more sophisticated software like Process Explorer.

If this is your problem, then you need to read about IDisposable and make sure you call Dispose on your objects after you're done with them (won't be rendered anymore). Bitmaps and Icons and most GDI+ objects implement IDisposable.

Furthermore, it's unclear to me, but you may need to call DeleteObject on some of the raw GDI objects themselves (all depends upon where you got their handles).

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

Check out this PInvoke sample, are you properly deleting the objects you are pulling in through unmanaged code?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文