修改代码以保存为jpeg

时间:2014-01-10 22:12:29

标签: c# bitmap jpeg

我有下面的代码,我希望它保存为jpeg文件格式,而不是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();
    }

你对我应该做些什么改变有什么想法吗?我尝试使用Image.Format,但这不起作用。

1 个答案:

答案 0 :(得分:3)

要将BitMap对象另存为JPG,请在保存方法中指定ImageFormat

currentSave(sdf.FileName, ImageFormat.Jpeg);

理想情况下,您可以将其基于用户选择的扩展名。类似于以下内容

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