WinForm chart management: resizing a chart when saving it to a file

Is there a way to resize the chart when using the method Chart.SaveImage()from the source code?

Currently, the only way to set the size of the chart is to resize the form on which the chart control ( System.Windows.Forms.DataVisualization.Charting.Chart) is sitting . Can I specify width and height? Trying to change Chart.Size, Chart.Widthor Chart.Sizedoes not work.

+3
source share
3 answers

Good. The solution was so obvious that I could not find it for 3 days - I installed it Chart.Dock = DockStyle.Fill, so changing the property is Sizenot affected. After changing it to DockStyle.NoneI could resize the chart and (finally!) Save it with a suitable width and height.

+3
source

You may have to save it to a memory stream, and then use the Image class to modify the dimensions, and then save it to a 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);
   }
}
0
source

, "" :

var ch = new Chart();
ch.Size = new Size(600, 250);
0

Source: https://habr.com/ru/post/1734317/


All Articles