我想在 Visual Studio 2013 中创建一个通用应用程序,该应用程序从Canvas
使用代码动态生成的图像创建图像。如果我在 UI 上显示画布(Grid
例如,在 a 中)就可以了。如果我不显示画布,应用程序会崩溃并显示以下消息:
值不在预期范围内。
有我使用的代码:
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
.
这是我的示例项目