AARRGGHHH
AARRGGHHH

Reputation: 147

Saving to a specific file format with SaveFileDialog in C#

I'm using a picture box to edit an image, and a file save dialog to save the image.

The problem I'm having is that regardless of the file format I select, the file is saved as a bitmap. I did some research here and tried making some changes, but they're not working. The dialog box tries to save the file 5 times, and then fails. My feeling is that I'm not successfully getting the file format/file extension selected.

I'm trying a couple different methods, but they're not working. I've read here on stack overflow that I should be using (Path.GetExtension(save.FileName)), but the compiler rejects Path.GetExtension (Path does not exist in the current context). Here's my code, I'd appreciate if someone could point out where my mistake is located.

{
    SaveFileDialog save = new SaveFileDialog();
    save.Filter = "Bitmap files (*.bmp)|*.bmp|JPG files (*.jpg)|*.jpg|GIF files (*.gif)|*.gif|PNG files (*.png)|*.png|TIF files (*.tif)|*.tif|All files (*.*)|*.*";
    save.FilterIndex = 4;
    save.RestoreDirectory = true;
    save.OverwritePrompt = true;
    save.ShowHelp = true;
    save.AddExtension = true;

    if (save.ShowDialog() == DialogResult.OK && save.FilterIndex == 1)  
        pictureBox1.Image.Save(save.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
    if (save.ShowDialog() == DialogResult.OK &&  save.FileName.Substring(save.FileName.Length - 4) == ".jpg") 
        pictureBox1.Image.Save(save.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
    if (save.ShowDialog() == DialogResult.OK && save.FileName.Substring(save.FileName.Length - 3) == "gif")
        pictureBox1.Image.Save(save.FileName, System.Drawing.Imaging.ImageFormat.Gif);
    if (save.ShowDialog() == DialogResult.OK && save.FileName.Substring(save.FileName.Length - 3) == "png") 
        pictureBox1.Image.Save(save.FileName, System.Drawing.Imaging.ImageFormat.Png);
    if (save.ShowDialog() == DialogResult.OK && save.FileName.Substring(save.FileName.Length - 3) == "tif")
        pictureBox1.Image.Save(save.FileName, System.Drawing.Imaging.ImageFormat.Tiff);
    else 
        MessageBox.Show("File Save Error."); 
}

Also, is there any way to access file save options in C# (for example, subsampling in jpg or dithering in gif)?

Thank you

Upvotes: -4

Views: 3996

Answers (1)

AARRGGHHH
AARRGGHHH

Reputation: 147

Just wanted to post what worked for whoever else comes along with the same question. Thanks to everyone above for your help.

using system.IO;

SaveFileDialog save = new SaveFileDialog();

save.Filter = "Bitmap files (*.bmp)|*.bmp|JPG files (*.jpg)|*.jpg|GIF files (*.gif)|*.gif|PNG files (*.png)|*.png|TIF files (*.tif)|*.tif|All files (*.*)|*.*";

save.FilterIndex = 2;

save.RestoreDirectory = true;

save.OverwritePrompt = true;

save.ShowHelp = true;

save.AddExtension = true;

if ((save.ShowDialog() == DialogResult.OK)) {
  if (Path.GetExtension(save.FileName).ToLower() == ".bmp") {
    pictureBox1.Image.Save(save.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
  }
  else if (Path.GetExtension(save.FileName).ToLower() == ".jpg") {
    pictureBox1.Image.Save(save.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
  }
  else if (Path.GetExtension(save.FileName).ToLower() == ".gif") {
    pictureBox1.Image.Save(save.FileName, System.Drawing.Imaging.ImageFormat.Gif);
  }
  else if (Path.GetExtension(save.FileName).ToLower() == ".png") {
    pictureBox1.Image.Save(save.FileName, System.Drawing.Imaging.ImageFormat.Png);
  }
  else if (Path.GetExtension(save.FileName).ToLower() == ".tif") {
    pictureBox1.Image.Save(save.FileName, System.Drawing.Imaging.ImageFormat.Tiff);
  }
  else {
    MessageBox.Show("File Save Error.");
  }
}

For some reason, PNGs are saving slightly larger than TIFs, PNGs should be about 33% smaller, I suspect that's an issue with the Visual Studio PNG implementation. For anyone interested in truly small PNGs, there's an open source console app called PNGcrush.

Upvotes: 1

Related Questions