/// <summary>
/// 压缩图片
/// </summary>
/// <param name=”filePath”></param>
/// <param name=”filePath_ystp”></param>
public void ystp(string filePath, string filePath_ystp) //
{
//Bitmap
Bitmap bmp = null;
//ImageCoderInfo
ImageCodecInfo ici = null;
//Encoder
Encoder ecd = null;
//EncoderParameter
EncoderParameter ept = null;
//EncoderParameters
EncoderParameters eptS = null;
try
{
bmp = new Bitmap(filePath);
ici = this.getImageCoderInfo(“image/jpeg”);
ecd = Encoder.Quality;
eptS = new EncoderParameters(1);
ept = new EncoderParameter(ecd, 65L);
/*Quality 类别指定图像的压缩级别。当用于构造 EncoderParameter 时,质量类别有效值的范围是从 0 到 100。指定的数字越小,
* 压缩的级别就越高,因而图像的质量也就越差。0 将导致最差质量的图像,而 100 将导致最佳质量的图像。
*/
eptS.Param[0] = ept;
bmp.Save(filePath_ystp, ici, eptS);
//bmp.Save(“d:\aa6.jpg”, ImageFormat.Jpeg);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
bmp.Dispose();
ept.Dispose();
eptS.Dispose();
}
}
/// <summary>
/// 获取图片编码类型信息
/// </summary>
/// <param name=”coderType”></param>
/// <returns></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;
}