寻找薛定谔的猫

导航

 
//参数: 原始图片,保存路径,压缩比
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);

 

posted on 2018-11-08 20:19  teagueli  阅读(1492)  评论(0编辑  收藏  举报