これに対する解決策は簡単だと確信していますが、頭を悩ませているので、誰かがここで光を当てることができるかもしれません.
私がやっていることは、プログラムの開始時に入力ファイルをプライベート変数 (_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