其他分享
首页 > 其他分享> > 压缩图片

压缩图片

作者:互联网

Code First

public bool ImageReduce(string oldFilePath, string newFilePath) 
        {

            Bitmap bmp = null;
            ImageCodecInfo ici = null;
            System.Drawing.Imaging.Encoder ecd = null;
            EncoderParameter ept = null;
            EncoderParameters eptS = null;
            try
            {

                bmp = new Bitmap(oldFilePath);
                ici = this.GetImageCoderInfo("image/jpeg");
                ecd = System.Drawing.Imaging.Encoder.Quality;
                eptS = new EncoderParameters(1);
                ept = new EncoderParameter(ecd, 150L);
                eptS.Param[0] = ept;
                bmp.Save(newFilePath, ici, eptS);

                bmp.Dispose();
                GC.Collect();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                bmp.Dispose();
                ept.Dispose();
                eptS.Dispose();
            }
        }



 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;
        }

快去动手试试吧

 

标签:ImageCodecInfo,ici,Dispose,压缩,eptS,bmp,null,图片
来源: https://www.cnblogs.com/A1999/p/15958759.html