压缩图片

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

快去动手试试吧

 

上一篇:Thinkphp6框架学习:有关数据库的基本操作


下一篇:区块链学习(3)