本文介绍了如何使用saveFileDialog在C#中保存图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

EDIT2:
I tryed this filter

Filter = bmp (*.bmp)|*.bmp|jpeg (*.jpeg)|*.jpeg|png (*.png)|*.png|tiff (*.tiff)|*.tiff
解决方案

You can use the SaveFileDialog like this:

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

这篇关于如何使用saveFileDialog在C#中保存图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 11:49