0

我敢肯定,解决这个问题很简单,但我只能摸不着头脑,所以也许有人可以在这里阐明一下。

我正在做的是当我的程序启动时,我将输入文件加载到私有变量(_bmpSource)中,并通过创建 ImageBrush 并设置画布的背景将其显示在画布中。在按钮上单击我通过旋转它来修改图像,然后通过再次设置画布的背景属性来显示它。看来我对内存管理不善,因为旋转/显示操作在几次迭代中都能正常工作,但最终会炸弹抱怨内存不足。

注意:我使用的图像是 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
4

1 回答 1

-1

GC 不知何故没有足够的时间来释放内存。在方法的末尾或每个循环中添加此代码。

System.GC.Collect()
System.GC.WaitForPendingFinalizers()
于 2013-11-14T02:22:39.673 回答