C#使用默认方法对图像进行保存的时候图像会有损失,以下提供无损保存的方法:
1 ///2 /// 无损保存图片 3 /// 4 /// 图片 5 /// 保存的路径 6 /// 图片的类型,image/jpeg,image/png,image/gif,image/tiff,image/bmp 7 ///8 public bool SaveImage(Image imgResult, string Path, string ImageType) 9 {10 EncoderParameter p;11 EncoderParameters ps;12 try13 {14 ps = new EncoderParameters(1);15 16 p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);17 ps.Param[0] = p;18 19 ImageCodecInfo ii = GetCodecInfo(ImageType);20 imgResult.Save(Path, ii, ps);21 imgResult.Dispose();22 }23 catch (Exception ex)24 {25 return false;26 }27 return true;28 }29 30 /// 31 /// 获取图片的编码类型32 /// 33 /// 34 ///35 public ImageCodecInfo GetCodecInfo(string mimeType)36 {37 ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();38 foreach (ImageCodecInfo ici in CodecInfo)39 {40 if (ici.MimeType == mimeType) return ici;41 }42 return null;43 }