3

I have the code below, and I'd like it to make it save as jpeg file format, instead of bmp.

Bitmap current = (Bitmap)_latestFrame.Clone();
        using (SaveFileDialog sfd = new SaveFileDialog())
        {
            sfd.Filter = "*.bmp|*.bmp";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                current.Save(sfd.FileName);
            }
        }

        current.Dispose();
    }

Do you have any idea on what I should change to make it? I tried using Image.Format, but that didn't work.

Arin Ghazarian
  • 5,105
  • 3
  • 23
  • 21
Sergiu Tripon
  • 103
  • 1
  • 2
  • 8

1 Answers1

3

To save a BitMap object as a JPG just specify the ImageFormat in the save method

currentSave(sdf.FileName, ImageFormat.Jpeg);

Ideally though you would base this off of the extension chosen by the user. Something like the following

sfd.Filter = "*.bmp|*.jpg:
if (sfd.ShowDialog() == DialogResult.OK) {
  if (sfd.FileName.EndsWith("jpg")) {
    current.Save(sfd.FileName, ImageFormat.Jpeg);
  } else { 
    current.Save(sfd.FileName);
  }
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454