2013-08-26 25 views
0

我確信解決這個問題的方法很簡單,但我留下了自己的腦袋,所以也許有人可以在這裏解釋一下。更新WPF畫布時的內存泄漏背景

我正在做的是當我的程序啓動時我通過創建一個ImageBrush並設置畫布的背景將一個輸入文件加載到一個私有變量(_bmpSource)並將其顯示在Canvas中。在一個按鈕上點擊我旋轉它來修改圖像,然後再次設置畫布的背景屬性來顯示它。看起來我對內存管理不善,因爲旋轉/顯示操作在幾次迭代中都能正常工作,但最終會炸彈抱怨內存不足。

注:我使用的圖像是5000x5000像素bmp。

我已經嘗試了一切從打破引用到調用垃圾回收,但它似乎仍然堅持這個數據以某種方式。

這裏是一些示例代碼,我可以重現問題。

私有變量的聲明:

Private _SourceFileName As String = "C:\Users\sean\Desktop\Input.bmp" 
Private _bmpSource As BitmapSource 

將調用此方法在我的構造對於圖像的初始加載/顯示:

Me._bmpSource = Me.LoadImage() 
Me._Canvas.Background = New System.Windows.Media.ImageBrush(Me._bmpSource) 

的LoadImage()實現:

Private Function LoadImage() As BitmapImage 

    Dim bmpImage As New BitmapImage 
    Dim imageSourceStream As New FileStream(Me._SourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read) 
    Dim decoder As New BmpBitmapDecoder(imageSourceStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad) 
    Dim encoder As New JpegBitmapEncoder 

    encoder.Frames.Add(BitmapFrame.Create(decoder.Frames(0))) 

    Dim streamSourcePath As String = My.Computer.FileSystem.GetTempFileName 
    Dim Stream As New System.IO.FileStream(streamSourcePath, FileMode.OpenOrCreate, FileAccess.Write) 

    encoder.Save(Stream) 
    Stream.Close() 
    Stream = Nothing 

    bmpImage.BeginInit() 
    bmpImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache 
    bmpImage.CacheOption = BitmapCacheOption.OnLoad 
    bmpImage.UriSource = New Uri(streamSourcePath) 
    bmpImage.EndInit() 

    Return bmpImage 
End Function 

以下是在按鈕單擊事件後面運行的RotateImage()實現:

Public Sub RotateImage() 

    ' Store our source rect for use in applied transforms 
    Dim ImageRect As New System.Drawing.RectangleF(System.Drawing.PointF.Empty, New System.Drawing.SizeF(Me._bmpSource.Width, Me._bmpSource.Height)) 

    ' Store our target rect for use in applied transforms 
    ' NOTE: For this demo we're always rotating 90 degrees so just flip height/width 
    Dim CanvasRect As System.Drawing.RectangleF = New System.Drawing.RectangleF(System.Drawing.Point.Empty, New System.Drawing.SizeF(Me._bmpSource.Height, Me._bmpSource.Width)) 

    ' Figure out the point in which we will need to draw the image 
    ' to fit it within the bounds of our canvas rectangle 
    Dim WidthOffset As Decimal = (CanvasRect.Width - ImageRect.Width)/2 
    Dim HeightOffset As Decimal = (CanvasRect.Height - ImageRect.Height)/2 

    ' Build transform needed for rotate 
    Dim transformActions As New System.Windows.Media.TransformGroup 
    transformActions.Children.Add(New System.Windows.Media.RotateTransform(90, Me._bmpSource.Width/2, Me._bmpSource.Height/2)) 
    transformActions.Children.Add(New System.Windows.Media.TranslateTransform(WidthOffset, HeightOffset)) 

    ' Apply the transformation to the image 
    Dim transformedBitmap As New System.Windows.Media.Imaging.TransformedBitmap(Me._bmpSource, transformActions) 

    ' Trying to break references here so things will get disposed but it isn't working 
    If TypeOf Me._bmpSource Is BitmapImage Then 
     CType(Me._bmpSource, BitmapImage).StreamSource = Nothing 
    ElseIf TypeOf Me._bmpSource Is TransformedBitmap Then 
     CType(Me._bmpSource, TransformedBitmap).Source = Nothing 
    End If 

    ' Breaking main ref also doesn't help 
    Me._bmpSource = Nothing 

    ' Garbage collection does nother either 
    GC.WaitForPendingFinalizers() 
    GC.Collect() 

    Me._bmpSource = transformedBitmap 
    Me._Canvas.Background = New System.Windows.Media.ImageBrush(Me._bmpSource) 
    Me._Canvas.UpdateLayout() 
End Sub 
+0

您可以擺脫TranslateTransform,因爲您告訴您的RotateTransform在圖像的中心旋轉。 – Nick

+0

我相信,如果我將圖像置於畫布中,那麼你會是正確的。它不會在此示例代碼中顯示它,但實際上我將左上角對齊,在這種情況下,需要翻譯。 – user2719109

回答

0

GC以某種方式沒有足夠的時間釋放內存。 將此代碼添加到方法的末尾或每個循環中。

System.GC.Collect() 
System.GC.WaitForPendingFinalizers()