【问题标题】:WPF Instantiate User control programmatically to render it as PNGWPF 以编程方式实例化用户控件以将其呈现为 PNG
【发布时间】:2010-11-30 11:44:16
【问题描述】:

我想在 DLL 中以编程方式实例化用户控件,然后将其保存为 PNG 文件。这对于 PngBitmapEncoder 和 RenderTargetBitmap 一般是没有问题的。

这是我的问题:

  • 如何实例化控件?只需使用 new-operator?
  • 我必须在单独的线程中实例化它吗?
  • 如何强制控件更新其所有子级并再次呈现自身?

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

    [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 文件只包含图表区域,但没有气泡?为什么?我认为这是一个更新/渲染问题,但是如何解决这个问题?

这是 Visual Studio 解决方案,它包含控制台项目,它将用户控件呈现为 PNG 文件和图表的 WPF 工具包的其他两个项目。

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

希望它没有问题!

谢谢!

【问题讨论】:

    标签: wpf controls refresh instantiation


    【解决方案1】:

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

    您的代码在控件创建后立即对其进行快照,因此您看不到任何数据。

    你可以:

    1. 把这一切都包装在一个窗口中,然后告诉 它在 X 之后拍摄快照 秒
    2. 禁用所有动画 在您要进行的任何控件中 快照
    3. 也许有办法 “快进”动画 以编程方式,但我不能 找一个

    这是解决方案 1,它有效:

        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();
        }
    }
    

    【讨论】:

    • 感谢您提供有趣的解决方案,但图表的生成应尽快完成,因此最好不要动画。为了覆盖动画,我简单地为气泡数据点创建了一个新的控制模板,但是现在图例项还有另一个问题。看看我的回答帖子!
    猜你喜欢
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多