Create WPF element offscreen and render to bitmap

杀马特。学长 韩版系。学妹 提交于 2019-11-26 23:03:25

问题


I can't understand why this doesn't work, or what I need to get it to work.

To repro, create a simple WPF application and replace the main window's constructor thusly:

    public MainWindow()
    {
        InitializeComponent();

        // simple visual definition
        var grid = new Grid { Width = 300, Height = 300 };
        var text = new TextBlock 
                       { 
                         Text = "Y DON'T I WORK???", 
                         FontSize = 100, 
                         FontWeight = 
                         FontWeights.Bold 
                       };
        grid.Children.Add(text);

        // update the layout so everything is awesome cool
        grid.Measure(grid.DesiredSize);
        grid.Arrange(new Rect(grid.DesiredSize));
        grid.UpdateLayout();

        // create a BitmapSource from the visual
        var rtb = new RenderTargetBitmap(
                                    (int)grid.Width,
                                    (int)grid.Height,
                                    96,
                                    96,
                                    PixelFormats.Pbgra32);
        rtb.Render(grid);

        // Slap it in the window
        this.Content = new Image { Source = rtb, Width = 300, Height = 300 };
    }

This results in an empty image. If I save the RTB to disk as a PNG its the correct size but transparent.

If, however, I do this with a visual that's been displayed on screen, it works fine.

How can I render a visual I've constructed offscreen to a bitmap?


回答1:


Because elements don't have a desired size until you measure them. You were telling the Grid to size itself with an available space of 0x0. Change your code to:

grid.Measure(new Size(grid.Width, grid.Height));
grid.Arrange(new Rect(new Size(grid.Width, grid.Height)));

(The call to UpdateLayout is unneeded.)



来源:https://stackoverflow.com/questions/1877115/create-wpf-element-offscreen-and-render-to-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!