winfrom 保存图片
1 private void btnSave_Click(object sender, EventArgs e) 2 { 3 SaveFileDialog sfd = new SaveFileDialog(); 4 sfd.Filter = "BMP (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPG (*.jpg)|*.jpg|PNG (*.png)|*.png|TIFF (*.tif)|*.tif"; 5 sfd.AddExtension = true; 6 if (sfd.ShowDialog() == DialogResult.OK) 7 { 8 SaveTypes savetype = SaveTypes.UNSPECIFIED; 9 switch (sfd.FilterIndex) 10 {
//图片格式 11 case 1: savetype = SaveTypes.BMP; break; 12 case 2: savetype = SaveTypes.GIF; break; 13 case 3: savetype = SaveTypes.JPG; break; 14 case 4: savetype = SaveTypes.PNG; break; 15 case 5: savetype = SaveTypes.TIFF; break; 16 default: break; 17 } 18 SaveImage(sfd.FileName, savetype, barcode.Image); 19 } 20 }
public enum SaveTypes : int { JPG, BMP, PNG, GIF, TIFF, UNSPECIFIED }
1 public void SaveImage(string Filename, SaveTypes FileType, Image image) 2 { 3 try 4 { 5 if (image != null) 6 { 7 ImageFormat imageformat; 8 switch (FileType) 9 { 10 case SaveTypes.BMP: imageformat = ImageFormat.Bmp; break; 11 case SaveTypes.GIF: imageformat = ImageFormat.Gif; break; 12 case SaveTypes.JPG: imageformat = ImageFormat.Jpeg; break; 13 case SaveTypes.PNG: imageformat = ImageFormat.Png; break; 14 case SaveTypes.TIFF: imageformat = ImageFormat.Tiff; break; 15 default: imageformat = ImageFormat.Bmp; break; 16 } 17 ((Bitmap)image).Save(Filename, imageformat); 18 } 19 } 20 catch (Exception ex) 21 { 22 throw new Exception(" 无法保存图像。" + ex.Message); 23 } 24 }