WPF 3.5 RenderTargetBitmap memory hog
Posted
by
kingRauk
on Stack Overflow
See other posts from Stack Overflow
or by kingRauk
Published on 2012-06-01T09:07:55Z
Indexed on
2012/06/01
10:41 UTC
Read the original article
Hit count: 1418
I have a 3.5 WPF application that use's RenderTargetBitmap.
It eat's memory like a big bear.
It's is a know problem in 3.5 that RenderTargetBitmap.Render has memory problems.
Have find some solutions for it, but i doesnt help. https://connect.microsoft.com/VisualStudio/feedback/details/489723/rendertargetbitmap-render-method-causes-a-memory-leak
Program takes too much memory And more...
Does anyway have any more ideas to solve it...
static Image Method(FrameworkElement e, int width, int height)
{
const int dpi = 192;
e.Width = width;
e.Height = height;
e.Arrange(new Rect(0, 0, width, height));
e.UpdateLayout();
if(element is Graph)
(element as Graph).UpdateComponents();
var bitmap = new RenderTargetBitmap((int)(width*dpi/96.0),
(int)(height*dpi/96.0),
dpi,
dpi,
PixelFormats.Pbgra32);
bitmap.Render(element);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
using (var stream = new MemoryStream())
{
encoder.Save(stream);
element.Clip = null;
Dispose(element);
bitmap.Freeze();
DisposeRender(bitmap);
bitmap.Clear();
GC.Collect();
GC.WaitForPendingFinalizers();
return System.Drawing.Image.FromStream(stream);
}
}
public static void Dispose(FrameworkElement element)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
public static void DisposeRender(RenderTargetBitmap bitmap)
{
if (bitmap != null) bitmap.Clear();
bitmap = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
© Stack Overflow or respective owner