使用下面的代码,我试图Canvas
用 UIElements 填充 a 并将其保存为 tif Image
。但是,我Image
的总是空白。这是因为Canvas
屏幕上从未显示过,并且从未发生过某种初始化和绘图?我怎样才能使这项工作?
创作将Canvas
是这样的:
Canvas theCanvas = new Canvas();
theCanvas.Width = 2740;
theCanvas.Height = 2280;
...
Button button = new Button();
button.Content = "Push Me.";
button.Height = 50;
button.Width = 200;
Canvas.SetTop(button, 200);
Canvas.SetLeft(button, 300);
theCanvas.Children.Add(button);
要创建Image
并保存它:
using (System.IO.FileStream fs =
new System.IO.FileStream(path, System.IO.FileMode.Create))
{
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
(int)inWidth,
(int)inHeight, 1 / 300, 1 / 300,
PixelFormats.Pbgra32);
DrawingVisual visual = new DrawingVisual();
using (DrawingContext context = visual.RenderOpen())
{
VisualBrush brush = new VisualBrush(inCanvas);
context.DrawRectangle(
brush,
null,
new Rect(new Point(), new Size(inWidth, inHeight)));
}
renderBitmap.Render(visual);
BitmapEncoder encoder = new TiffBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
encoder.Save(fs);
fs.Close();
}