Snapshot a WPF canvas area using RenderTargetBitmap

I want to take a snapshot of the canvas area in my application. I use a visual brush to take a snapshot and save it using PngEncoder. But the resulting PNG is just a blank black image. I am not sure if the problem is related to the created BitmapSource or PNGEncoder problem. Here is the code that I use to get the same.

public void ConvertToBitmapSource(UIElement element) { var target = new RenderTargetBitmap((int)(element.RenderSize.Width), (int)(element.RenderSize.Height), 96, 96, PixelFormats.Pbgra32); var brush = new VisualBrush(element); var visual = new DrawingVisual(); var drawingContext = visual.RenderOpen(); drawingContext.DrawRectangle(brush, null, new Rect(new Point(0, 0), new Point(element.RenderSize.Width, element.RenderSize.Height))); drawingContext.Close(); target.Render(visual); PngBitmapEncoder encoder = new PngBitmapEncoder(); BitmapFrame outputFrame = BitmapFrame.Create(target); encoder.Frames.Add(outputFrame); using (FileStream file = File.OpenWrite("TestImage.png")) { encoder.Save(file); } } 
+4
source share
2 answers

Why VisualBrush? Since each UIElement is visual, you can simply pass the element directly to the Render method:

 public void ConvertToBitmapSource(UIElement element) { var target = new RenderTargetBitmap( (int)element.RenderSize.Width, (int)element.RenderSize.Height, 96, 96, PixelFormats.Pbgra32); target.Render(element); var encoder = new PngBitmapEncoder(); var outputFrame = BitmapFrame.Create(target); encoder.Frames.Add(outputFrame); using (var file = File.OpenWrite("TestImage.png")) { encoder.Save(file); } } 
+8
source

Thank you for the question and answer.

In the interest of others seeking the same answer.

I found that Clemens' path leaves a black bar in the image when the image is shifted either down or to the right. As if he did not display the element in the correct position in the bitmap.

So I had to use VisualBrush, as Amar suggested.

Here is the code that worked for me:

  RenderTargetBitmap RenderVisual(UIElement elt) { PresentationSource source = PresentationSource.FromVisual(elt); RenderTargetBitmap rtb = new RenderTargetBitmap((int)elt.RenderSize.Width, (int)elt.RenderSize.Height, 96, 96, PixelFormats.Default); VisualBrush sourceBrush = new VisualBrush(elt); DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); using (drawingContext) { drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(elt.RenderSize.Width, elt.RenderSize.Height))); } rtb.Render(drawingVisual); return rtb; } 
+7
source

All Articles