1

In my WPF project I use the Control.Datavisualization.Charting.Chart control to plot some line series.

I also would like to store the Chart into an image, so I do the following (after series creation):

        this.chart.Series.Clear();
        this.chart.Series.Add(lineCurve);
        this.chart.Series.Add(lineBilinear);


        //store the image file in folder
        DrawUtils.saveChartToPng(this.chart, path);

where saveChartToPng() is as follows:

public static void saveChartToPng(Chart chart, String filename)
        {
            Rect bounds = VisualTreeHelper.GetDescendantBounds(chart);
            double dpi = 96d;


            RenderTargetBitmap rtb = new RenderTargetBitmap((int)bounds.Width, (int)bounds.Height, dpi, dpi, System.Windows.Media.PixelFormats.Default);


            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext dc = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(chart);
                dc.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
            }

            rtb.Render(dv);

            //endcode as PNG
            BitmapEncoder pngEncoder = new PngBitmapEncoder();
            pngEncoder.Frames.Add(BitmapFrame.Create(rtb));

            //save to memory stream
            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            pngEncoder.Save(ms);
            ms.Close();
            System.IO.File.WriteAllBytes(filename, ms.ToArray());

            return;
        }

After the saveChartToPng() the chart is saved to an image, but series are not present in the image. Series are visible in the chart inside the window.

What am I missing before calling the saveChartToPng()?

Taterhead
  • 5,763
  • 4
  • 31
  • 40
FrancescoDS
  • 1,077
  • 4
  • 21
  • 55

0 Answers0