2015-10-15 60 views
0

我在這個智慧的結尾。我有一個返回圖標的函數。我的問題是,當應用程序正在運行,正在使用的功能,我可以看我的應用程序內存使用攀升。幾分鐘後,我會得到一個「在GDI +中發生的通用錯誤」,這會導致我的應用程序崩潰。 我已經通過論壇,並找到了建議,但我沒有試過似乎解決了這個問題。在函數的末尾添加.dispose()會延長髮生這種情況的時間長度,但不會使其停止。 這裏是一個有我的問題代碼:創建圖標時的通用gdi +錯誤

Private Function drawIcon(ByVal frameColor As Color, ByVal numColor As Color, ByVal capColor As Color, ByVal scrColor As Color) 
    Dim ti As Icon 
    Dim bmp As New Bitmap(16, 16) 
    Dim bmpgraphics As Graphics = Graphics.FromImage(bmp) 
    Using bmpgraphics 
     Dim framePen As New Pen(frameColor) 
     Dim numBrush As New SolidBrush(numColor) 
     Dim capBrush As New SolidBrush(capColor) 
     Dim scrBrush As New SolidBrush(scrColor) 
     Dim strip As Integer = 64/3 
     'outside 
     Dim numFrame As Rectangle = New Rectangle(0, 0, strip, 16 - 1) 
     Dim capFrame As Rectangle = New Rectangle(strip, 0, strip, 16 - 1) 
     Dim scrFrame As Rectangle = New Rectangle(strip * 2, 0, strip, 16 - 1) 
     ' inside 
     Dim numInside As Drawing.Rectangle = New Rectangle(1, 1, strip - 1, 16 - 2) 
     Dim capInside As Drawing.Rectangle = New Rectangle(strip + 1, 1, strip - 1, 16 - 2) 
     Dim scrInside As Drawing.Rectangle = New Rectangle(strip * 2 + 1, 1, strip - 1, 16 - 2) 
     ' do the drawing 
     With bmpgraphics 
      .DrawRectangle(framePen, numFrame) 
      .DrawRectangle(framePen, capFrame) 
      .DrawRectangle(framePen, scrFrame) 
      .FillRectangle(numBrush, numInside) 
      .FillRectangle(capBrush, capInside) 
      .FillRectangle(scrBrush, scrInside) 
     End With 
     'Dim tmpBmp As New Bitmap(bmp) 
     'ti = Drawing.Icon.FromHandle(tmpBmp.GetHicon) 
     'tmpBmp.Dispose() 
     ti = Drawing.Icon.FromHandle(bmp.GetHicon) 
    End Using 
    bmp.Dispose() 
    bmpgraphics.Dispose() 
    Return ti 
End Function 
+0

問題的不一部分,但打開選項嚴格。那些pend和畫筆是需要處理的資源(或''使用'd),Icon返回也是一種資源。當用戶代碼完成後,它也需要處理。 – Plutonix

+0

今天在網站上有一個類似的錯誤,它有圖表 - 它已經運行了幾個月,從來沒有出現過問題。做了一個iisreset,它再次工作。還不確定,如果這只是IIS的暫時性打嗝,或者我需要解決某些問題,但是我認爲如果它有幫助,我會提及它。 –

+0

謝謝你們。我想我現在已經舔了它。我偶然發現了另一個網站上的修復程序。 – 123Andrew321

回答

0

發現在其他網站上此修復程序。沒有想到書籤,似乎也無法將其挖掘出歷史。有一次,我添加了DestroyIcon函數我有我的程序啓動和運行8小時以上內存佔用上升,然後

Private Declare Auto Function DestroyIcon Lib "user32" (ByVal hIcon As IntPtr) As Boolean 

    Private Function drawIcon(ByVal frameColor As Color, ByVal numColor As Color, ByVal capColor As Color, ByVal scrColor As Color) 
    Dim bmp As New Bitmap(16, 16) 
    Dim bmpgraphics As Graphics = Graphics.FromImage(bmp) 
    Using bmpgraphics ' As Graphics = Graphics.FromImage(pb.Image) 
     Dim ti As Icon 
     Dim framePen As New Pen(frameColor) 
     Dim numBrush As New SolidBrush(numColor) 
     Dim capBrush As New SolidBrush(capColor) 
     Dim scrBrush As New SolidBrush(scrColor) 
     Dim strip As Integer = 64/3 
     'outside 
     Dim numFrame As Rectangle = New Rectangle(0, 0, strip, 16 - 1) 
     Dim capFrame As Rectangle = New Rectangle(strip, 0, strip, 16 - 1) 
     Dim scrFrame As Rectangle = New Rectangle(strip * 2, 0, strip, 16 - 1) 
     ' inside 
     Dim numInside As Drawing.Rectangle = New Rectangle(1, 1, strip - 1, 16 - 2) 
     Dim capInside As Drawing.Rectangle = New Rectangle(strip + 1, 1, strip - 1, 16 - 2) 
     Dim scrInside As Drawing.Rectangle = New Rectangle(strip * 2 + 1, 1, strip - 1, 16 - 2) 
     ' do the drawing 
     With bmpgraphics 
      .DrawRectangle(framePen, numFrame) 
      .DrawRectangle(framePen, capFrame) 
      .DrawRectangle(framePen, scrFrame) 
      .FillRectangle(numBrush, numInside) 
      .FillRectangle(capBrush, capInside) 
      .FillRectangle(scrBrush, scrInside) 
     End With 
     'Dim tmpBmp As New Bitmap(bmp) 
     'ti = Drawing.Icon.FromHandle(tmpBmp.GetHicon) 
     'tmpBmp.Dispose() 
     'ti = Drawing.Icon.FromHandle(bmp.GetHicon) 

     Dim hicon As IntPtr = bmp.GetHicon() 
     Dim bitmapIcon As Icon = Icon.FromHandle(hicon) 
     DestroyIcon(bitmapIcon.Handle) 
     Return bitmapIcon 
    End Using 
    bmp.Dispose() 
    bmpgraphics.Dispose() 
End Function 
0

另一種方式,可以一次建設圖標的數組,在程序啓動,然後得到從那裏需要的圖標,而不是每次從位圖轉換它。

Module MyModule 
    Public IconList16() As Icon 

    Sub BuildIconList(SourceImgList as ImageList) 
     ' Builds up Icon List from provided ImageList 
     Dim q As Int16 
     Dim bmp As Bitmap = Nothing 

     ReDim IconList16(MainForm.Icons16.Images.Count - 1) 
     For q = 0 To UBound(IconList16) 
      bmp = SourceImgList.Images(q) 
      IconList16(q) = Icon.FromHandle(bmp.GetHicon) 
     Next 
     bmp.Dispose() 

    End Sub 

    (...) 

    Sub AnySub() 
     Dim IconID as Integer 

     ' Initialize IconList16 
     Call BuildIconList(Form1.ImageList1)     
     (...) 
      (do stuff) 
     (...) 
     IconID = [**ChosenValue**]      ' Just make sure IconID is set to a value which is in IconList16 bounds... 
     AnySuitableObject.Icon = Iconlist16(IconID)  ' AnySuitableObject: any object, having .Icon property, of course. 

End Sub 

前端模塊

相關問題