I have an issue where I am using WPF to apply a System.Windows.Media.Effects.Effect to a System.Drawing.Bitmap using the following:
vb Code:
Partial Public Class ImageFilter
Public Sub ApplyWPFEffect(ByVal Effect As System.Windows.Media.Effects.Effect)
OnFilterStarted(r)
Dim ImageSource As New System.Windows.Media.Imaging.BitmapImage()
Using ms As New IO.MemoryStream()
b.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
ImageSource.BeginInit()
ImageSource.StreamSource = ms
ImageSource.EndInit()
Dim FormImage As New System.Windows.Controls.Image()
FormImage.Effect = Effect
FormImage.Source = ImageSource
Dim ImageSize = New System.Windows.Size(b.Width, b.Height)
FormImage.Measure(ImageSize)
Dim renderingRectangle As New System.Windows.Rect(ImageSize)
'QUITE LEAKY >>>
FormImage.Arrange(renderingRectangle)
Dim enc As New System.Windows.Media.Imaging.PngBitmapEncoder
'XTREMELY LEAKY!!!! >>>
Dim bmpSource As New System.Windows.Media.Imaging.RenderTargetBitmap(b.Width, b.Height, 96, 96, System.Windows.Media.PixelFormats.Pbgra32)
bmpSource.Render(FormImage)
enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmpSource))
Using msOut As New IO.MemoryStream
enc.Save(msOut)
Using bOut As New Bitmap(msOut)
Using g As Graphics = Graphics.FromImage(b)
g.Clear(Color.Transparent)
g.DrawImage(bOut, 0, 0, bOut.Width, bOut.Height)
End Using
End Using
End Using
End Using
OnFilterFinished()
End Sub
End Class
If I loop this code the memory keeps growing ...
What could be going on?
EDIT: The really leaky lines now have comments before them...
I have also tried methods suggested online about calling clear and freeze on it with no success.
Thanks in advance,
Kris