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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With