WPF以编程方式实例化用户控件以将其呈现为PNG [英] WPF Instantiate User control programmatically to render it as PNG

查看:143
本文介绍了WPF以编程方式实例化用户控件以将其呈现为PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在DLL中以编程方式实例化用户控件,然后将其另存为PNG文件。通常PngBitmapEncoder和RenderTargetBitmap没问题。

I want to instantiate a user control programmatically in a DLL to save it afterwards as PNG file. This is generally no problem with PngBitmapEncoder and RenderTargetBitmap.

这是我的问题:


  • 如何实例化控件?只需使用new-operator?

  • 是否必须在单独的线程中实例化它?

  • 如何强制控件更新所有控件?孩子并再次渲染自己?

这是我的代码,用于实例化用户控件并将其另存为PNG文件(LetterFrequency是用户控制):

This is my code to instantiate the user control and save it as PNG-file (LetterFrequency is the user control):

    [STAThread]
    static void Main(string[] args)
    {
        LetterFrequency let = new LetterFrequency();
        let.Width = 600;
        let.Height = 400;
        let.Background = Brushes.White;

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

        let.UpdateLayout();

        RenderTargetBitmap bitmap = new RenderTargetBitmap((int)let.Width, (int)let.Height, 96d, 96d, PixelFormats.Pbgra32);
        bitmap.Render(let);

        PngBitmapEncoder png = new PngBitmapEncoder();
        png.Frames.Add(BitmapFrame.Create(bitmap));

        using (Stream stm = File.Create("test.png"))
        {
            png.Save(stm);
        }
    }

如果以此方式运行应用程序,它将生成PNG文件,但是将看不到将添加到XAML中的数据,如果您查看XAML设计器,则可以看到带有一些气泡的图表。 png文件仅包含图表区域,而没有气泡?为什么?我认为这是一个更新/渲染问题,但是如何解决呢?

If you run the app this way, it generates the PNG file, but the data, which will be added in the XAML are not visible, if you look into the XAML Designer, you can see the chart with some bubbles. The png file contains only the chart area, but no bubbles? Why that? I think is is a Update/rendering problem, but how to solve this?

这是Visual Studio解决方案,它包含Console Project,该项目将用户控件呈现为

Here is the visual studio solution, it contains the Console Project, which renders the user control to a PNG file and two other projects of the WPF toolkit for the chart.

看一下它,它将分别在bin / Debug中生成PNG文件。 exe文件夹: http://www.file-upload.net/download -1904406 / ChartRenderBitmap.zip.html

Have a look at it, it will generate the PNG file in the bin/Debug respectively in the exe-folder: http://www.file-upload.net/download-1904406/ChartRenderBitmap.zip.html

希望它能正常工作!

谢谢!

推荐答案

图表中的数据未呈现在PNG文件中,因为有一个动画应用于显示数据点。看看您在Window中的LetterFrequency控件,您会发现这些点逐渐暴露出来。

The data in your chart is not rendered in your PNG file because there is an animation applied to the reveal of the data points. Take a look at your LetterFrequency control in a Window, and you'll see the points gradually reveal themselves.

您的代码在创建控件后会立即对其进行快照,

Your code takes a snapshot of the control immediately after its creation, so you see no data.

您可以:


  1. 将所有内容包装在其中一个窗口,告诉
    它在X
    秒后拍摄快照

  2. 在要转到$ b $的任何控件中禁用所有动画
    b快照

  3. 也许可以通过编程方式来实现
    快进动画
    的方式,但是我无法
    找到一个

这里的解决方案1可行:

Here's solution 1, and it works:

    public partial class Window1 : Window
{
    System.Windows.Threading.DispatcherTimer snapshotTimer;

    public Window1()
    {
        InitializeComponent();

        this.Width = 600;
        this.Height = 400;
        let.Width = 600;
        let.Height = 400;
        let.Background = Brushes.White;     

        this.Loaded += new RoutedEventHandler(Window1_Loaded);
    }

    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
        this.snapshotTimer = new System.Windows.Threading.DispatcherTimer();
        this.snapshotTimer.Interval = TimeSpan.FromSeconds(2);
        this.snapshotTimer.Tick += new EventHandler(snapshotTimer_Tick);
        this.snapshotTimer.IsEnabled = true;
    }

    void snapshotTimer_Tick(object sender, EventArgs e)
    {
        this.snapshotTimer.IsEnabled = false;
        WritePng();
    }

    void WritePng()
    {
        RenderTargetBitmap bitmap = new RenderTargetBitmap((int)let.Width, (int)let.Height, 96d, 96d, PixelFormats.Pbgra32);
        bitmap.Render(let);

        PngBitmapEncoder png = new PngBitmapEncoder();
        png.Frames.Add(BitmapFrame.Create(bitmap));

        using (Stream stm = File.Create("test.png"))
        {
            png.Save(stm);
        }

        this.Close();
    }
}

这篇关于WPF以编程方式实例化用户控件以将其呈现为PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆