以下代码提示用户选择从pictureBox保存图像的路径:
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Portable Network Graphics|*.png";
saveFileDialog1.Title = "Bild speichern";
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
this.picBox.Image.Save(myStream.ToString()); // is not getting the selected path
myStream.Close();
}
}
但是如何从myStream获取路径或将图像保存到用户定义的位置(兼容.NET 3.5)?
答案 0 :(得分:1)
如果要从保存对话框中获取所选文件路径,请使用...
saveFileDialog1.FileName;
有关此属性的详细信息,请参阅here
您无需担心使用Stream执行此任务。
为了清楚起见,这是你的代码应该是......
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Portable Network Graphics|*.png";
saveFileDialog1.Title = "Bild speichern";
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
this.picBox.Image.Save(saveFileDialog1.FileName);
}
答案 1 :(得分:1)
你只能使用SaveFileDialog.FileName
,不需要分开的流,试试这个:
using (var saveFileDialog1 = new SaveFileDialog())
{
saveFileDialog1.Filter = "Portable Network Graphics|*.png";
saveFileDialog1.Title = "Bild speichern";
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
picBox.Image.Save(saveFileDialog1.FileName);
}
}
答案 2 :(得分:0)
您可以使用:
string path = Path.GetDirectory(saveFileDialog1.Filename);
this.picBox.Image.Save(saveFileDialog1.Filename);
你真的不需要一个流来做到这一点:)
答案 3 :(得分:0)
这是怎么回事?
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Portable Network Graphics|*.png";
saveFileDialog1.Title = "Bild speichern";
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
(using FileStream fStr = new FileStream(saveFileDialog1.FileName, FileMode.Create))
{
this.picBox.Image.Save(fStr);
fStr.Close();
}
}