In WinRT C #, how to save an off-screen XAML tree using RenderTargetBitmap?

The following code throws a System.ArgumentException critical exception from the RenderAsync "Value is not in the expected range" method. If, on the other hand, my canvas is part of the visible XAML tree, it works. Is it impossible to display any XAML that does not appear on the screen?

Canvas c = new Canvas(); c.Width = 40; c.Height = 40; c.Background = new SolidColorBrush(Color.FromArgb(0xff, 0x80, 0xff, 0x80)); RenderTargetBitmap x = new RenderTargetBitmap(); await x.RenderAsync(c); 

I almost thought this answer would work, but no luck, I suppose this only applies to WPF: Create a WPF element behind the scenes and display a bitmap

UPDATE:

So far, my best idea is to put the canvas that I want to display on the current visible page, but put it under what is usually the root UIElement that fills the screen so that it is not visible to the user:

 <Grid> <Canvas x:Name="HiddenCanvas"/> <Grid x:Name="mainElement" Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> </Grid> </Grid> 

It is not beautiful, but it seems to work. Let's see if anyone can do better

+6
source share
2 answers

Satur9nine's solution for locating the displayed UI tree somewhere behind an opaque foreground seems to be the only supported solution. You can also play with the opacity of the parent element to avoid its appearance. Another option is to do it yourself using Direct2D or use something like the WinRTXamlToolkit.Composition.Render() methods from the WinRT XAML Toolkit.

+1
source

WinRTXamlToolkit.Composition The namespace has this extension that works. Just call this method:

 await WriteableBitmapRenderExtensions.RenderToPngStream(element); 
+1
source

Source: https://habr.com/ru/post/952424/


All Articles