【发布时间】:2014-11-07 20:48:47
【问题描述】:
我想将我的图像保存到用户选择的文件夹中。我知道我可以使用流输出。但似乎更容易从 windows 提供的保存文件对话框中获取用户选择的路径,然后在 Image 类的保存函数中使用该路径。
Image img1;
String path;
...
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
// Directory to open when saving files
saveFileDialog1.InitialDirectory = path;
// Sets the title of your dialog box
saveFileDialog1.Title = "Save";
// Sets the file type to filter out
saveFileDialog1.Filter = "png files (*.png)|*.png|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
//Destroys file when done if set to false
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
img1.Save(HERE);
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not save file too disk. " + ex.Message);
}
}
}
我用“HERE”表示字符串目录路径的位置。
【问题讨论】:
-
您查看过
SaveFileDialog的文档吗? -
有时Google 会很有帮助。