Search code examples
c#imagecontrolsmicrosoft-chart-controls

SaveImage of ChartControl without display It in the screen


I wanna save a ChartControl to an Image without display it in the screen.

var theChart = new Chart();
var theSeries = new Series("values");
theSeries.IsVisibleInLegend = false;
theChart.Series.Add(theSeries);
theSeries.Points.AddXY(1, 1);
theSeries.Points.AddXY(2, 2);
theSeries.Points.AddXY(3, 3);
theChart.SaveImage(@"D:\Téléchargements\HiddenChart4.png", ChartImageFormat.Png);

Then I get a blank picture. I think it's because the control is not paint. Is it possible ?


Solution

  • Here the answer with the missing part

    var theChart = new Chart();
    
            //missing part
            var chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            theChart.ChartAreas.Add(chartArea1);
    
            var theSeries = new Series("values");
            theSeries.IsVisibleInLegend = false;
            theChart.Series.Add(theSeries);
            theSeries.Points.AddXY(1, 1);
            theSeries.Points.AddXY(2, 2);
            theSeries.Points.AddXY(3, 3);
            theChart.SaveImage(@"D:\Téléchargements\HiddenChart6.png", ChartImageFormat.Png);