如何将图表从WPF工具包(MS Chart)导出到PNG。它不起作用,它只创建一个黑色的PNG

时间:2012-04-12 22:33:53

标签: c# wpf png wpftoolkit mschart

我无法将MS Chart(从WPF工具包)导出到PNG。我从不同的论坛跟随一步,但在一切之后,我的PNG完全是黑色的。我做错了什么?

private void export_graf_Click(object sender, RoutedEventArgs e)
        {
            if (mcChart.Series[0] == null)
            {
                MessageBox.Show("there is nothing to export");
            }
            else
            {

           RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)mcChart.ActualWidth,    (int)mcChart.ActualHeight, 95d, 95d, PixelFormats.Pbgra32);


                renderBitmap.Render(mcChart);

                Microsoft.Win32.SaveFileDialog uloz_obr = new Microsoft.Win32.SaveFileDialog();
                uloz_obr.FileName = "Graf";
                uloz_obr.DefaultExt = "png";


                Nullable<bool> result = uloz_obr.ShowDialog();
                if (result == true)
                {
                    string obr_cesta = uloz_obr.FileName; //cesta k souboru

              using (FileStream outStream = new FileStream(obr_cesta, FileMode.Create))
                    {
                        PngBitmapEncoder encoder = new PngBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                        encoder.Save(outStream);
                    }

                }

1 个答案:

答案 0 :(得分:3)

我认为您遇到了布局问题。 RenderTargetBitmap类适用于可视层,其中包括从其可视父级继承的偏移量和变换。在将可视元素渲染为BitmapFrame时,应将其隔离。除非您想要透明背景,否则您还可以指定背景颜色而不会影响窗口的可视树。 PNG格式支持Alpha透明度,一些图像查看器将透明像素显示为黑色。

WPF的默认dpi是96.我不确定你为什么指定95.这不是零绑定索引或类似的东西。以下示例使用96dpi。

private void export_graf_Click(object sender, RoutedEventArgs e)
{
    if (mcChart.Series[0] == null)
    {
        MessageBox.Show("there is nothing to export");
    }
    else
    {
        Rect bounds = VisualTreeHelper.GetDescendantBounds(mcChart);

        RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)bounds.Width, (int)bounds.Height, 96, 96, PixelFormats.Pbgra32);

        DrawingVisual isolatedVisual = new DrawingVisual();
        using (DrawingContext drawing = isolatedVisual.RenderOpen())
        {
            drawing.DrawRectangle(Brushes.White, null, new Rect(new Point(), bounds.Size)); // Optional Background
            drawing.DrawRectangle(new VisualBrush(mcChart), null, new Rect(new Point(), bounds.Size));
        }

        renderBitmap.Render(isolatedVisual);

        Microsoft.Win32.SaveFileDialog uloz_obr = new Microsoft.Win32.SaveFileDialog();
        uloz_obr.FileName = "Graf";
        uloz_obr.DefaultExt = "png";

        Nullable<bool> result = uloz_obr.ShowDialog();
        if (result == true)
        {
            string obr_cesta = uloz_obr.FileName;

            using (FileStream outStream = new FileStream(obr_cesta, FileMode.Create))
            {
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                encoder.Save(outStream);
            }
        }
    }
}