2009-09-03 118 views
1

我寫這是越來越使用user32.dllGetIconInfo功能當前光標的圖標信息的應用程序,它正常工作了一段時間,但一段時間後,它開始在ICONINFO.hbmMask(一些負值提供錯誤信息),並在下一行我嘗試從Bitmap.HBitmap(bitmask)得到位圖對象,它拋出一個異常:的GetIconInfo功能不能正常工作

A Generic error occured in GDI+. 

從那裏onwords,它不斷地給這個例外,因爲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 
+1

嘗試發佈一些代碼。 – 2009-09-03 05:54:17

+0

也許這篇文章有幫助:[調試GDI資源泄漏](https://blogs.msdn.microsoft.com/oldnewthing/20170519-00/?p=96195) – coz 2017-05-24 04:39:39

回答

0

看看這個PInvoke樣,你是正確刪除您是通過非託管代碼拉動的對象?

1

你的循環有多大? GDI +資源是操作系統資源,可用性受到限制。

如果這是您的問題,您可以通過監控您的流程分配的HANDLE來找出。如果GDI +在特定句柄計數(HBITMAP或HICON)達到限制時開始抱怨,那麼您知道必須更加智能地處理資源。你可以先使用任務管理器來做到這一點,但可能想切換到更復雜的軟件,如Process Explorer

如果這是您的問題,那麼您需要閱讀有關IDisposable,並確保您在對象完成後調用Dispose(不再呈現)。位圖和圖標以及大多數GDI +對象實現IDisposable。此外,我還不清楚,但是您可能需要在某些原始GDI對象本身上調用DeleteObject(全部取決於您獲得手柄的位置)。

+0

我需要連續捕獲圖標,它工作正常一段時間,然後它開始給這個例外,永遠不會回來.. – 2009-09-03 06:30:46

+0

那麼問題是,你在哪裏釋放資源?我的最初陳述是,你的資源有限。即使您持續加載它們,機器也沒有無限的資源。 – 2009-09-03 06:54:55

+0

謝謝弗蘭克,但你可以請給我一些代碼示例告訴我如何釋放資源,我可以使用DeleteObject gdi32.dll ...我在C#環境.. – 2009-09-03 07:01:13