如何将图像从 Stream 对象保存到选定的路径
以下代码提示用户选择保存图片框中图像的路径:
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 兼容)?
The following code prompts the user to select a path to save an image from the 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();
}
}
But how can I get the path from myStream or save the image to the user defined location (with compatibility to .NET 3.5)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想从保存对话框中获取选定的文件路径,请使用...
请参阅 此处 了解有关此属性的更多信息
您无需担心是否使用 Stream 来完成此任务。
需要明确的是,您的代码应该是这样的......
If you want to get the selected file path from the save dialog then use...
See here for more information on this property
You don't need to worry about using a Stream for this task.
Just to be clear, here is what your code should be...
您可以仅使用
SaveFileDialog.FileName
,不需要单独的流,请尝试以下操作:you can work with the
SaveFileDialog.FileName
only, no need for separated streams, try this:您可以使用:
您真的不需要流来做到这一点:)
You can use:
You really don't need a stream to do that :)
这怎么样?
How's this?