如何使用 saveFileDialog 在 C# 中保存图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11055258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:18:35  来源:igfitidea点击:

How to use saveFileDialog for saving images in C#?

c#imagesavefiledialog

提问by user1097772

Possible Duplicate:
Issue while saving image using savefiledialog

可能重复:
使用 savefiledialog 保存图像时出现问题

I use windows forms in C#. How should I use saveFileDialog? I have picturebox and on the picture box there is an image and I want to save it. Loaded image is bmp. I want to save it as one of 4 formats: bmp, jpeg, png, tiff. I read some some notes on MDSN and also tryed it but I probably do something wrong. So I better ask how should be it write? How should be wrote method private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)and how should look like property saveFileDialog.Filter? Thanks

我在 C# 中使用 windows 窗体。我应该如何使用 saveFileDialog?我有图片框,图片框上有一张图片,我想保存它。加载的图像是 bmp。我想将它保存为 4 种格式之一:bmp、jpeg、png、tiff。我读了一些关于 MDSN 的笔记,也尝试过,但我可能做错了什么。所以我最好问一下它应该怎么写?应该如何编写方法private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)以及属性saveFileDialog.Filter应该如何写 ?谢谢

EDIT:
What I've tryed:
Issue while saving image using savefiledialog

编辑:
我尝试过的:
使用 savefiledialog 保存图像时出现问题

EDIT2:
I tryed this filter

EDIT2:
我试过这个过滤器

Filter = bmp (*.bmp)|*.bmp|jpeg (*.jpeg)|*.jpeg|png (*.png)|*.png|tiff (*.tiff)|*.tiff

采纳答案by John Koerner

You can use the SaveFileDialog like this:

您可以像这样使用 SaveFileDialog:

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Images|*.png;*.bmp;*.jpg";
ImageFormat format = ImageFormat.Png;
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    string ext = System.IO.Path.GetExtension(sfd.FileName);
    switch (ext)
    {
        case ".jpg":
            format = ImageFormat.Jpeg;
            break;
        case ".bmp":
            format = ImageFormat.Bmp;
            break;
    }
    pictureBox1.Image.Save(sfd.FileName, format);
}