使用c#調整圖片質量

//參數: 原始圖片,保存路徑,壓縮比
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;
 }

調用ui

CompressImage(img, toPath, 90L);
相關文章
相關標籤/搜索