使用c#调整图片质量
2021-07-02 10:07
标签:net top 参数 raw enc ima parameter form com 调用 使用c#调整图片质量 标签:net top 参数 raw enc ima parameter form com 原文地址:https://www.cnblogs.com/yaotome/p/9931768.html//参数: 原始图片,保存路径,压缩比
private void CompressImage(MagickNet.Image img,string toPath, long ratio)
{
using (Bitmap thumbImage = new Bitmap(MagickNet.Image.ToBitmap(img)))
{
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, ratio);
myEncoderParameters.Param[0] = myEncoderParameter;
thumbImage.Save(toPath, jgpEncoder, myEncoderParameters);
}
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
CompressImage(img, toPath, 90L);