4

I want to store a canvas on disk as a PNG image.

This works when the canvas is completely shown using this code:

RenderTargetBitmap rtb = new RenderTargetBitmap(794, 1122, 96d, 96d, System.Windows.Media.PixelFormats.Default);
rtb.Render(canvas);

But parts that are not shown at that time are not rendered. When the visual is not shown at all, nothing is rendered.

Then how can I save Canvas build in code to disc without showing it?

Tiele Declercq
  • 2,070
  • 2
  • 28
  • 39

1 Answers1

3

Okay, I've got it fixed with the help of heltonbiker & Clemens.

var size = new Size(794, 122);
Document.Measure(size);
Document.Arrange(new Rect(size));

Document.UpdateLayout();

RenderTargetBitmap rtb = new RenderTargetBitmap(794, 1122, 96d, 96d, System.Windows.Media.PixelFormats.Default);
rtb.Render(Document);

But when I save my canvas to disc it had a black background. I solved this by adding a Rectangle to my XAML inside the Canvas.

Tiele Declercq
  • 2,070
  • 2
  • 28
  • 39