我正在使用 savefiledialog 来保存图像。画布是图片框,加载的图像是位图。当我尝试保存它时,文件已创建但不知何故损坏。因为当我尝试再次加载图像或在不同的查看器中显示时它不起作用 - 我的意思是保存的文件已损坏。有一种保存图像的方法。
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
System.IO.FileStream fs =
(System.IO.FileStream)saveFileDialog1.OpenFile();
try
{
switch (saveFileDialog1.FilterIndex)
{
case 1:
canvas.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 2:
canvas.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case 3:
canvas.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);
break;
case 4:
canvas.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Tiff);
break;
}
}
catch (Exception ex)
{
System.Console.WriteLine("Exception " + ex);
}
我还应该提到属性过滤器。saveFileDialog1.Filter 具有值:
bmp (*.bmp)|*.bmp|jpeg (*.jpeg)|*.jpeg|png (*.png)|*.png|tiff (*.tiff)|*.tiff