WinForm图表控件:将图表保存到文件时更改图表的大小

发布于 2024-08-22 19:58:31 字数 272 浏览 3 评论 0原文

使用源代码中的方法 Chart.SaveImage() 时,有没有办法更改图表的大小?

目前,我发现设置图表大小的唯一方法是调整图表控件(System.Windows.Forms.DataVisualization.Charting.Chart)所在窗体的大小。我可以显式设置其宽度和高度吗?尝试更改 Chart.SizeChart.WidthChart.Size 不起作用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半边脸i 2024-08-29 19:58:31

好的。解决方案非常明显,我三天都找不到它 - 我设置了 Chart.Dock = DockStyle.Fill,因此更改 Size 属性不会产生影响。将其修改为 DockStyle.None 后,我可以更改图表的大小并(最后!)以适当的宽度和高度保存它。

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.

酷炫老祖宗 2024-08-29 19:58:31

您可以通过重新定义图表的 Size 属性来定义它:

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

You can define it by redefining the Size property of the chart :

var ch = new Chart();
ch.Size = new Size(600, 250);
鹿! 2024-08-29 19:58:31

您可能必须将其保存到内存流,然后使用 Image 类更改尺寸,然后将其保存到文件。

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);
   }
}

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);
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文