RenderTargetBitmap.Render Method
Renders the Visual object.
Assembly: PresentationCore (in PresentationCore.dll)
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Use this DisposeExtension Method to avoid MemoryLeaks
///
<summary>
$0 /// Disposes the specified RenderTargetBitmap.
$0
$0 /// </summary>
$0
$0 /// <param name="bitmap">The
RenderTargetBitmap.</param>
$0
$0 public static void Dispose(this RenderTargetBitmap bitmap) {
$0
$0 if (bitmap != null) bitmap.Clear();
$0
$0 bitmap = null;
$0
$0 GC.Collect();
$0
$0 GC.WaitForPendingFinalizers();
$0
$0 }$0
- 3/7/2012
- Pascal Szorath
- 3/7/2012
- Pascal Szorath
Is there memory leak in this method?
public void CreateControl(string filePath, Visual SourceControl, double resolution)
{
using (FileStream outStream = new FileStream(filePath, FileMode.Create))
{
JpegBitmapEncoder enc = new JpegBitmapEncoder();
// BitmapSource source = CreateBitmapFromVisual(SourceControl, resolution, resolution);
Rect bounds = VisualTreeHelper.GetContentBounds(SourceControl);
RenderTargetBitmap rtb = new RenderTargetBitmap((Int32)(bounds.Width * resolution / 96.0), (Int32)(bounds.Height * resolution / 96.0), resolution, resolution, PixelFormats.Pbgra32);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext dc = dv.RenderOpen())
{
VisualBrush vb = new VisualBrush(SourceControl);
dc.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
vb = null;
}
rtb.Render(dv);
// BitmapFrame bitmapFrame = BitmapFrame.Create(rtb);
// enc.Frames.Add(bitmapFrame);
// enc.Save(outStream);
// bitmapFrame = null;
rtb = null;
enc = null;
outStream.Dispose();
}
}
{
using (FileStream outStream = new FileStream(filePath, FileMode.Create))
{
JpegBitmapEncoder enc = new JpegBitmapEncoder();
// BitmapSource source = CreateBitmapFromVisual(SourceControl, resolution, resolution);
Rect bounds = VisualTreeHelper.GetContentBounds(SourceControl);
RenderTargetBitmap rtb = new RenderTargetBitmap((Int32)(bounds.Width * resolution / 96.0), (Int32)(bounds.Height * resolution / 96.0), resolution, resolution, PixelFormats.Pbgra32);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext dc = dv.RenderOpen())
{
VisualBrush vb = new VisualBrush(SourceControl);
dc.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
vb = null;
}
rtb.Render(dv);
// BitmapFrame bitmapFrame = BitmapFrame.Create(rtb);
// enc.Frames.Add(bitmapFrame);
// enc.Save(outStream);
// bitmapFrame = null;
rtb = null;
enc = null;
outStream.Dispose();
}
}
- 12/31/2010
- 佛笑了