Convert Chart object to Image object C#

821 views Asked by At

I want to convert a chart object to a Image object, or if it is not possible then save the chart to a .jpg file. I need this because I want the chart put in to a PDFSharp object..

So this is my sample code so far:

Chart chart1 = new Chart();

            chart1.Series.Add("series1");
            chart1.Series.Add("series2");
            chart1.Series.Add("series3");

            chart1.Series["series1"].ChartType = SeriesChartType.Column;
            chart1.Series["series2"].ChartType = SeriesChartType.Line;
            chart1.Series["series3"].ChartType = SeriesChartType.Area;

            Random random = new Random();

            for (int pointIndex = 0; pointIndex < 10; pointIndex++)
            {

                chart1.Series["series1"].Points.AddY(random.Next(20, 50));
            }

            for (int pointIndex = 0; pointIndex < 10; pointIndex++)
            {

                chart1.Series["series2"].Points.AddY(random.Next(50, 100));
            }

            for (int pointIndex = 0; pointIndex < 10; pointIndex++)
            {

                chart1.Series["series3"].Points.AddY(random.Next(70, 100));
            }

            chart1.SaveImage(@".\test", ImageFormat.Jpeg);

Because of a reason i dont know the 'chart1.SaveImage(@".\test", ImageFormat.Jpeg);' puts out a empty file

1

There are 1 answers

0
Boot750 On

This post is about f-sharp but i think you could have the same problem. FSharpChart.SaveAs () saves blank image if called before chart rendering is complete

it looks like that the chart first have to be displayed before you can save it.