views:

372

answers:

2

Hi, is there a way to change size of chart when using method Chart.SaveImage() from the source code?

Right now the only way I found to set the size of chart, is resize the form on which chart control (System.Windows.Forms.DataVisualization.Charting.Chart) sits. Can I explicit set its width and height? Trying to change Chart.Size, Chart.Width or Chart.Size doesn't work.

A: 

You'll probably have to save it to a memory stream, then use the Image class to change dimensions and then save it to file.

using(MemoryStream ms = new MemoryStream(4096))
{
   myChart.SaveImage(ms,ImageFormat.Png);
   using(Bitmap img = Image.FromStream(ms))
   {
     using(Graphics g = Graphics.FromImage(img))
       g.DrawImage( b, 0, 0, newWidth, newHeight );
     }
     img.Save("where\to\save\chart.png",ImageFormat.Png);
   }
}
scottm
Still no effect. Image has the same width/hight like original chart.
pascon
A: 

All right. The solution was so obvious that I couldn't found it thou 3 days - I had setted Chart.Dock = DockStyle.Fill, so changing Size property doesn't affect. After modified it to DockStyle.None I could change chart's size and (finally!) save it with appropriative width and height.

pascon