1

I want to save image from chart graphic. I'm using following line.

chart.SaveImage("D:\\mypic.png", System.Web.UI.DataVisualization.Charting.ChartImageFormat.Png);

but I have a problem:

enter image description here

Here is my code.

protected void btnBar_Click(object sender, EventArgs e)
{
    string degerler = txtDeger.Text;
    ArrayList array = new ArrayList();

    if (Convert.ToInt32(TextBox2.Text) > Convert.ToInt32(TextBox1.Text))
    {
        Label3.Text = "TopNCount sayısı büyük olamaz...";
    }
    else
    {
        Label3.Text = "";

        for (int i = 1; i < Convert.ToInt32(TextBox1.Text) + 1; i++)
        {
            array.Add(Convert.ToInt32(degerler.Split(',')[i - 1]));
        }

        chart.ChartType = myChart.ChartTypes.Bar;
        chart.TopNCount = Convert.ToInt32(TextBox2.Text);
        chart.DataSource = array;
        chart.DataBind();
        chart.SaveImage("D:\\mypic.png", System.Web.UI.DataVisualization.Charting.ChartImageFormat.Png);
        this.Controls.Add(chart);    
    }
}
Berkay Turancı
  • 3,373
  • 4
  • 32
  • 45
TheMuyu
  • 579
  • 2
  • 12
  • 31

2 Answers2

1

my guess is that you can't save the chart as image because nothing has been rendered. try to hook to the Init/ Load / DataBound events and try to save it from there.

EDIT

try the following:

chart.DataBound += (sender, args) => ((Chart)sender).SaveImage("D:\\mypic.png", System.Web.UI.DataVisualization.Charting.ChartImageFormat.Png);

or

chart.Load += (sender, args) => ((Chart)sender).SaveImage("D:\\mypic.png", System.Web.UI.DataVisualization.Charting.ChartImageFormat.Png);
esskar
  • 10,638
  • 3
  • 36
  • 57
  • hi sorry i cant understand what you are talking about. can you give me more information. and chart is diplay in aspx file but not save. – TheMuyu Nov 29 '11 at 00:58
1

Someone else had that problem:

http://social.msdn.microsoft.com/Forums/en/MSWinWebChart/thread/6ca0a897-4ffe-4c67-851e-6002ee4af19d

It seems that the chart control wasn't able to locate the background used for the chart.

If that's the problem, maybe you could report the incorrect exception message on the Microsoft Connect website.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69