WPF UserControl的服务器端呈现

Run*_*sen 15 c# wpf xaml console-application oxyplot

我正在C#/ .Net 4.5中编写一个服务器端控制台应用程序,它可以获取一些数据并创建保存以供Web服务器显示的静态图表图像.

我主要使用这里描述的方法:http: //lordzoltan.blogspot.com/2010/09/using-wpf-to-render-bitmaps.html

但是,我添加了一个mainContainer.UpdateLayout(); 在Arrange()之后,数据绑定会更新并在渲染图像中可见,以及在它之前的一个Measure()......啊,我不会去那里.

以下是执行渲染的方法:

void RenderAndSave(UIElement target, string filename, int width, int height)
{
    var mainContainer = new Grid
    {
        HorizontalAlignment = HorizontalAlignment.Stretch,
        VerticalAlignment = VerticalAlignment.Stretch
    };

    mainContainer.Children.Add(target);

    mainContainer.Measure(new Size(width, height));
    mainContainer.Arrange(new Rect(0, 0, width, height));
    mainContainer.UpdateLayout();

    var encoder = new PngBitmapEncoder();
    var render = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);

    render.Render(mainContainer);
    encoder.Frames.Add(BitmapFrame.Create(render));
    using (var s = File.Open(filename, FileMode.Create))
    {
        encoder.Save(s);
    }
}
Run Code Online (Sandbox Code Playgroud)

该方法的目标参数将是我所做的WPF/XAML UserControl的一个实例 - 此时相当简单,只是一个网格,其中一些文本数据绑定到我分配给DataContext的ViewModel对象.

对于OxyPlot Plot对象,磁盘上保存的图像看起来很好 - 它完全是白色的.

现在,当我在Visual Studio 2013中担任设计师时,我可以看到它.我添加了一个设计时DataContext,它与我在运行时使用的对象相同(这是我正在做的一个尖峰 - viewmodel还没有最终形式,只是在我解决问题时有一堆默认数据).在设计师中,我看到了OxyPlot绘制的图表.

为了让我的渲染也包含这个OxyPlot图表,我还需要做些什么吗?这或多或少是练习的重点,所以实际让它出现真是太棒了!

提前感谢任何见解和建议!

jsa*_*ics 5

如果您在运行时也正确绑定数据,那么它应该可以工作.

在此输入图像描述

 [STAThread]
 static void Main(string[] args)
 {
     string filename = "wpfimg.png";

     RenderAndSave(new UserControl1(), filename, 300, 300);

     PictureBox pb = new PictureBox();
     pb.Width = 350;
     pb.Height = 350;
     pb.Image = System.Drawing.Image.FromFile(filename);

     Form f = new Form();
     f.Width = 375;
     f.Height = 375;
     f.Controls.Add(pb);
     f.ShowDialog();
 }
Run Code Online (Sandbox Code Playgroud)

XAML:

<UserControl x:Class="WpfApp92.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:oxy="http://oxyplot.org/wpf"
             xmlns:local="clr-namespace:WpfApp92"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <Grid>
        <oxy:PlotView Model="{Binding Model}"/>
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

CS:

public partial class UserControl1 : UserControl
{
    public PlotModel Model { get; set; }

    public UserControl1()
    {
        InitializeComponent();

        Model = new PlotModel();
        Model.LegendBorderThickness = 0;
        Model.LegendOrientation = LegendOrientation.Horizontal;
        Model.LegendPlacement = LegendPlacement.Outside;
        Model.LegendPosition = LegendPosition.BottomCenter;
        Model.Title = "Simple model";
        var categoryAxis1 = new CategoryAxis();
        categoryAxis1.MinorStep = 1;
        categoryAxis1.ActualLabels.Add("Category A");
        categoryAxis1.ActualLabels.Add("Category B");
        categoryAxis1.ActualLabels.Add("Category C");
        categoryAxis1.ActualLabels.Add("Category D");
        Model.Axes.Add(categoryAxis1);
        var linearAxis1 = new LinearAxis();
        linearAxis1.AbsoluteMinimum = 0;
        linearAxis1.MaximumPadding = 0.06;
        linearAxis1.MinimumPadding = 0;
        Model.Axes.Add(linearAxis1);
        var columnSeries1 = new ColumnSeries();
        columnSeries1.StrokeThickness = 1;
        columnSeries1.Title = "Series 1";
        columnSeries1.Items.Add(new ColumnItem(25, -1));
        columnSeries1.Items.Add(new ColumnItem(137, -1));
        columnSeries1.Items.Add(new ColumnItem(18, -1));
        columnSeries1.Items.Add(new ColumnItem(40, -1));
        Model.Series.Add(columnSeries1);
        var columnSeries2 = new ColumnSeries();
        columnSeries2.StrokeThickness = 1;
        columnSeries2.Title = "Series 2";
        columnSeries2.Items.Add(new ColumnItem(12, -1));
        columnSeries2.Items.Add(new ColumnItem(14, -1));
        columnSeries2.Items.Add(new ColumnItem(120, -1));
        columnSeries2.Items.Add(new ColumnItem(26, -1));
        Model.Series.Add(columnSeries2);

        DataContext = this;
    }
}
Run Code Online (Sandbox Code Playgroud)