使用 SaveFileDialog 保存图表图像

Saving an Image of Chart With a SaveFileDialog

单击按钮时,我的程序会捕获图表的屏幕截图,标题为 chartMain。代码如下:

private void buttonScreenshot_Click(object sender, EventArgs e)
{
    this.chartMain.SaveImage("C:/capture.png", ChartImageFormat.Png);
}

但是,这不允许用户指定保存位置,所以我想知道如何实现 SaveFileDialog 以允许用户选择保存位置。

有人能给我指出正确的方向吗,因为我目前被卡住了。

提前致谢。

这应该为您解决:

SaveFileDialog dialog = new SaveFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
   this.chartMain.SaveImage(dialog.FileName, ChartImageFormat.Png);
}

一个简单的例子,根据您的需要进行修改(不过我会查看 this 页面):

var save = new SaveFileDialog();
save.Filter = "PNG files (*.png)|*.txt|All files (*.*)|*.*";
if(save.ShowDialog() == DialogResult.OK)
{
    this.chartMain.SaveImage(save.FileName, ChartImageFormat.Png);
}