How to use saveFileDialog for saving images in C#? [duplicate]

怎甘沉沦 提交于 2019-11-29 10:29:38

问题


Possible Duplicate:
Issue while saving image using 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

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

回答1:


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


来源:https://stackoverflow.com/questions/11055258/how-to-use-savefiledialog-for-saving-images-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!