I want to convert a xaml to a png image using c#. I used RenderTargetBitmap. It works quite well if the xaml that's meant to be converted is displayed in a window or a page and you can actually see it on screen. But if the window is closed or hidden, a blank image will be generated. Does anyone know why this happens or how to make it work?
Asked
Active
Viewed 386 times
2 Answers
1
What I would suggest you:
Putting it on screen for the duration of your capture and then remove it. If the object is small it may even appear and disappear in no more than a flicker.
If you aren't fine with that you can try to Force Render a WPF Control using this:
use a ViewBox to render in memory
Grid grid = new System.Windows.Controls.Grid() { Background = Brushes.Blue, Width = 200, Height = 200 };
Viewbox viewbox = new Viewbox();
viewbox.Child = grid; //control to render
viewbox.Measure(new System.Windows.Size(200, 200));
viewbox.Arrange(new Rect(0, 0, 200, 200));
viewbox.UpdateLayout();
RenderTargetBitmap render = new RenderTargetBitmap(200, 200, 150, 150, PixelFormats.Pbgra32);
render.Render(viewbox);