Reputation: 56
I want to create a universal app in Visual Studio 2013 that creates an image from a Canvas
that is generated with code dynamically. If I display the canvas on the UI (in a Grid
, for example) it's alright. If I don't display the canvas, the app crashes with the message:
value does not fall within the expected range.
There is the code that i use:
Canvas cn = new Canvas();
cn.Height = 100;
cn.Width = 100;
cn.Visibility = Visibility.Visible;
Button but = new Button();
Rectangle rect = new Rectangle();
rect.Width = 100;
rect.Height = 100;
rect.Margin = new Thickness(0);
SolidColorBrush bruch = new SolidColorBrush();
bruch.Color = Colors.Blue;
rect.Fill = bruch;
but.Width = 50;
but.Height = 50;
but.Content = "23";
but.Visibility = Visibility.Visible;
cn.Children.Add(rect);
cn.Children.Add(but);
but.Margin = new Thickness(25, 25, 0, 0);
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(cn, 100, 100);
outputImage.Source = renderTargetBitmap;
I don't want to display the canvas on the UI because I plan to use this code from a BackgroundTask
.
Here is my sample project
Upvotes: 1
Views: 1129
Reputation: 21899
Windows Phone apps can use the XamlRenderingBackgroundTask to render in a background task. C++ is recommended for the background task to keep resource usage low.
See the Updating a tile from a background task sample for an example.
XamlRenderingBackgroundTask is not supported for Windows Store apps. On Windows the RenderTargetBitmap cannot be used from a background task and requires that the target UIElement be in the main visual tree.
Upvotes: 4