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

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

         /// <summary>
/// 无损保存图片
/// </summary>
/// <param name="imgResult">图片</param>
/// <param name="Path">保存的路径</param>
/// <param name="ImageType">图片的类型,image/jpeg,image/png,image/gif,image/tiff,image/bmp</param>
/// <returns></returns>
public bool SaveImage(Image imgResult, string Path, string ImageType)
{
EncoderParameter p;
EncoderParameters ps;
try
{
ps = new EncoderParameters(); p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
ps.Param[] = p; ImageCodecInfo ii = GetCodecInfo(ImageType);
imgResult.Save(Path, ii, ps);
imgResult.Dispose();
}
catch (Exception ex)
{
return false;
}
return true;
} /// <summary>
/// 获取图片的编码类型
/// </summary>
/// <param name="mimeType"></param>
/// <returns></returns>
public ImageCodecInfo GetCodecInfo(string mimeType)
{
ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo ici in CodecInfo)
{
if (ici.MimeType == mimeType) return ici;
}
return null;
}
上一篇:Hibernate五 HQL查询


下一篇:Hibernate之HQL查询