首页 > 代码库 > C#图像处理(5):无损保存图片

C#图像处理(5):无损保存图片

C#使用默认方法对图像进行保存的时候图像会有损失,以下提供无损保存的方法:

 1         /// <summary> 2         /// 无损保存图片 3         /// </summary> 4         /// <param name="imgResult">图片</param> 5         /// <param name="Path">保存的路径</param> 6         /// <param name="ImageType">图片的类型,image/jpeg,image/png,image/gif,image/tiff,image/bmp</param> 7         /// <returns></returns> 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         /// <summary>31         /// 获取图片的编码类型32         /// </summary>33         /// <param name="mimeType"></param>34         /// <returns></returns>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         }