如何将图像从 Stream 对象保存到选定的路径

发布于 2024-12-12 04:09:54 字数 791 浏览 0 评论 0原文

以下代码提示用户选择保存图片框中图像的路径:

        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 技术交流群。

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

发布评论

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

评论(4

岁月流歌 2024-12-19 04:09:55

如果您想从保存对话框中获取选定的文件路径,请使用...

saveFileDialog1.FileName;

请参阅 此处 了解有关此属性的更多信息

您无需担心是否使用 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);
}

If you want to get the selected file path from the save dialog then use...

saveFileDialog1.FileName;

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 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);
}
怂人 2024-12-19 04:09:55

您可以仅使用 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);
    }
}

you can work with the SaveFileDialog.FileName only, no need for separated streams, try this:

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);
    }
}
三人与歌 2024-12-19 04:09:55

您可以使用:

string path = Path.GetDirectory(saveFileDialog1.Filename);
this.picBox.Image.Save(saveFileDialog1.Filename);

您真的不需要流来做到这一点:)

You can use:

string path = Path.GetDirectory(saveFileDialog1.Filename);
this.picBox.Image.Save(saveFileDialog1.Filename);

You really don't need a stream to do that :)

是你 2024-12-19 04:09:55

这怎么样?

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

How's this?

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