Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render a hidden canvas without showing it?

Tags:

.net

wpf

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?

like image 790
Tiele Declercq Avatar asked Oct 20 '25 03:10

Tiele Declercq


1 Answers

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.

like image 193
Tiele Declercq Avatar answered Oct 26 '25 19:10

Tiele Declercq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!