C# 降低图片的质量
/// <summary>
/// 压缩图片
/// </summary>
/// <param name="filePath">要压缩的图片的路径</param>
/// <param name="newfilePath">压缩后的图片的路径</param>
public void ChangeImageSize(string filePath, string newfilePath)
{
Bitmap bmp = null;
ImageCodecInfo ici = null;
System.Drawing.Imaging.Encoder ecd = null;
EncoderParameter ept = null;
EncoderParameters eptS = null;
try
{
bmp = new Bitmap(filePath);
ici = this.getImageCoderInfo("image/jpeg");
ecd = System.Drawing.Imaging.Encoder.Quality;
eptS = new EncoderParameters(1);
ept = new EncoderParameter(ecd, 80L);
eptS.Param[0] = ept;
bmp.Save(newfilePath, ici, eptS);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
bmp.Dispose();
ept.Dispose();
eptS.Dispose();
}
}
/// <summary>
/// 获取图片编码类型信息
/// </summary>
/// <param name="coderType">编码类型</param>
/// <returns>ImageCodecInfo</returns>
private ImageCodecInfo getImageCoderInfo(string coderType)
{
ImageCodecInfo[] iciS = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo retIci = null;
foreach (ImageCodecInfo ici in iciS)
{
if (ici.MimeType.Equals(coderType))
retIci = ici;
}
return retIci;
}