winfrom 保存图片
Posted Bonnie_W
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
以上是关于winfrom 保存图片的主要内容,如果未能解决你的问题,请参考以下文章
Winfrom开发之OpenFileDialog选中图片即时保存至项目中