使用InteractiveDataDisplay.WPF导出图形

时间:2018-05-17 14:38:53

标签: c# wpf data-binding charts wpf-controls

我使用InteractiveDataDisplay.WPF组件生成图形,一切顺利,但当我尝试将图形导出到图像时,系列无法正确导出图形。

关注我的代码和图片:

<Grid Name="LayoutRoot" Background="White">
    <Grid Name="Grafico">
        <d3:Chart Name="plotter">
            <Grid Name="lines"/>
        </d3:Chart>
    </Grid>
</Grid>
public MainWindow()
    {
        InitializeComponent();

        double[] x = new double[200];
        for (int i = 0; i < x.Length; i++)
            x[i] = 3.1415 * i / (x.Length - 1);

        for (int i = 0; i < 25; i++)
        {
            var lg = new LineGraph();
            lines.Children.Add(lg);
            lg.Stroke = new SolidColorBrush(Color.FromArgb(255, 0, (byte)(i * 10), 0));
            lg.Description = String.Format("Data series {0}", i + 1);
            lg.StrokeThickness = 2;
            lg.Plot(x, x.Select(v => Math.Sin(v + i / 10.0)).ToArray());
        }

        int width = 800;
        int heigth = 600;

        Chart Bmain_Chart = new Chart();
        Bmain_Chart.Measure(new Size(width, heigth));
        Bmain_Chart.Arrange(new Rect(new Size(width, heigth)));
        Bmain_Chart.LeftTitle = "Bmain";
        Bmain_Chart.BottomTitle = "Time";
        Bmain_Chart.Content = lines;
        Bmain_Chart.UpdateLayout();

        RenderTargetBitmap bmp = new RenderTargetBitmap(width, heigth, 96, 96, PixelFormats.Pbgra32);
        bmp.Render(Bmain_Chart);

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

        using (Stream stm = File.Create(@"c:\MyCustomPath\test.png")) { encoder.Save(stm); }
    }

正确的图形

Correct Graphic

导出的图片

Exported Graphic

如果有人能帮助我,我将永远感激。

1 个答案:

答案 0 :(得分:1)

在当前版本的IDD中,许多IDD组件都依赖于“ OnLoaded”事件。 因此,您需要在窗口中的某处放置一个图表以正确对其进行初始化。

有解决此问题的方法。

在加载idd组件后,应渲染为bmp。 因此,下一个代码可以正常工作:

public partial class MainWindow : Window
{
    protected override void OnContentRendered(EventArgs e)
    {

        double width = 800;
        double heigth = 600;

        RenderTargetBitmap bmp = new RenderTargetBitmap((int)width, (int)heigth, 96, 96, PixelFormats.Pbgra32);
        bmp.Render(plotter);

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

        using (Stream stm = File.Create(@"c:\MyCustomPath\test.png")) { encoder.Save(stm); }
    }

    public MainWindow()
    {
        InitializeComponent();

        double[] x = new double[200];
        for (int i = 0; i < x.Length; i++)
            x[i] = 3.1415 * i / (x.Length - 1);

        for (int i = 0; i < 25; i++)
        {
            var lg = new LineGraph();
            lines.Children.Add(lg);
            lg.Stroke = new SolidColorBrush(Color.FromArgb(255, 0, (byte)(i * 10), 0));
            lg.Description = String.Format("Data series {0}", i + 1);
            lg.StrokeThickness = 2;
            lg.Plot(x, x.Select(v => Math.Sin(v + i / 10.0)).ToArray());
        }
    }
}

如果确实不需要向用户显示组件,则可以将this.Hide(); 添加到窗口的构造函数中。否则,您可以将渲染代码添加到某个按钮的OnClick处理程序中。