0
votes

I have generated a Piechart using MS chart Library.Now as per my requirement i have to generate a image of this chart and later use this image in my application.Here at present i am generating image through hard coded image name and path Which i dont Want to do.I want image to be generated and saved into Some Variable which i will use later in my application.

Here is my code in c# to generate image..

pieChart.SaveImage(@"D:\MyImage.jpg", System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Jpeg);

Please help me . Thanks in advance.

2

2 Answers

5
votes

Create a Bitmap variable

Private Bitmap chartImage;

Save the chart to memory stream and then create a bitmap with the memory stream.

using (MemoryStream ms = new MemoryStream())
{
    pieChart.SaveImage(ms, ChartImageFormat.Jpeg);
    this.chartImage= new Bitmap(ms);
}
1
votes

The base Chart class has the DrawToBitmap() method.

In case your chart is too big, catch the exception and use a temporary file with System.IO.Path.GetTempFileName();.