将“不可见”WPF控件渲染为位图图像

时间:2022-05-18 20:54:20

Render a WPF control to a bitmap image is not a trivial task as I found out today. As I know now dealing with the parent control margin is a problem, as Rick Strahl wrote in his blog

如今我发现,将WPF控件渲染为位图图像并不是一项简单的任务。据我所知,正如Rick Strahl在他的博客中写道的那样处理父控制边缘是一个问题

http://www.west-wind.com/weblog/posts/2007/Sep/10/Rendering-a-WPF-Container-to-Bitmap

http://www.west-wind.com/weblog/posts/2007/Sep/10/Rendering-a-WPF-Container-to-Bitmap

So far I am able to create bitmaps of any control that is visible inside a window but what I really need to do is create bitmaps of invisible controls. I just create them in code - simples shapes like rectangle and ellipse - and want to save them as bitmaps to disk. For me this turn out to be a personal nightmare. Since my ActualHeight and ActualWidth is always 0 I use Height and Width instead. But all I get is a empty image in the size of my control.

到目前为止,我能够创建窗口内可见的任何控件的位图,但我真正需要做的是创建不可见控件的位图。我只是在代码中创建它们 - 简单的形状,如矩形和椭圆形 - 并希望将它们保存为磁盘的位图。对我来说,这是个人的噩梦。由于我的ActualHeight和ActualWidth始终为0,因此我使用高度和宽度。但我得到的只是一个空白的图像,大小与我无关。

How can I create a bitmap of any control without adding it to a visible window?

如何在不将其添加到可见窗口的情况下创建任何控件的位图?

1 个解决方案

#1


20  

New elements haven't had their layout performed. You need to call Measure and Arrange on the control before you render it.

新元素尚未执行其布局。在渲染之前,需要在控件上调用Measure and Arrange。

Canvas c = new Canvas();

Rectangle r = new Rectangle
{
    Fill = Brushes.Orange,
    Width = 200,
    Height = 100
};

Ellipse e = new Ellipse
{
    Fill = Brushes.DodgerBlue,
    Width = 100,
    Height = 100
};

Canvas.SetLeft(e, 150);

c.Children.Add(r);
c.Children.Add(e);

var size = new Size(250,100);
c.Measure(size);
c.Arrange(new Rect(size));

RenderTargetBitmap bmp = new RenderTargetBitmap(250, 100, 96, 96, PixelFormats.Pbgra32);

bmp.Render(c);

PngBitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bmp));

using(var s = File.OpenWrite("image.png"))
    enc.Save(s);

#1


20  

New elements haven't had their layout performed. You need to call Measure and Arrange on the control before you render it.

新元素尚未执行其布局。在渲染之前,需要在控件上调用Measure and Arrange。

Canvas c = new Canvas();

Rectangle r = new Rectangle
{
    Fill = Brushes.Orange,
    Width = 200,
    Height = 100
};

Ellipse e = new Ellipse
{
    Fill = Brushes.DodgerBlue,
    Width = 100,
    Height = 100
};

Canvas.SetLeft(e, 150);

c.Children.Add(r);
c.Children.Add(e);

var size = new Size(250,100);
c.Measure(size);
c.Arrange(new Rect(size));

RenderTargetBitmap bmp = new RenderTargetBitmap(250, 100, 96, 96, PixelFormats.Pbgra32);

bmp.Render(c);

PngBitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bmp));

using(var s = File.OpenWrite("image.png"))
    enc.Save(s);