[英]wpf, c#, renderTargetBitmap of viewport3D without assigning it to a window
[英]RenderTargetBitmap without putting it on UI
我想在Visual Studio 2013中创建一个通用应用程序,该应用程序可以通过使用代码动态生成的Canvas
创建图像。 如果我在UI上显示画布(例如,在Grid
),就可以了。 如果我不显示画布,则应用程序崩溃并显示以下消息:
值不在预期范围内。
我使用的代码是:
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;
我不想在UI上显示画布,因为我打算使用BackgroundTask
代码。
这是我的示例项目
Windows Phone应用程序可以使用XamlRenderingBackgroundTask在后台任务中进行渲染。 建议将C ++用于后台任务,以保持较低的资源使用率。
Windows Store应用程序不支持XamlRenderingBackgroundTask。 在Windows上,RenderTargetBitmap不能从后台任务中使用,并且要求目标UIElement在主视觉树中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.