c#實現圖片高質量壓縮


參考:https://www.cnblogs.com/ZXdeveloper/p/6841878.html

由於參考的博文提供的代碼在我的pc上跑起來,會使壓縮后的圖片有白色邊框,所以我做了小小的修改

        /// <summary>
        /// 無損壓縮圖片
        /// </summary>
        /// <param name="sFile">原圖片地址</param>
        /// <param name="dFile">壓縮后保存圖片地址</param>
        /// <param name="flag">壓縮質量(數字越小壓縮率越高)1-100</param>
        /// <param name="size">壓縮后圖片的最大大小</param>
        /// <param name="sfsc">是否是第一次調用</param>
        public static bool CompressImage(string sFile, string dFile, int flag = 90, int size = 300, bool sfsc = true)
        {
            //如果是第一次調用,原始圖像的大小小於要壓縮的大小,則直接復制文件,並且返回true
            FileInfo firstFileInfo = new FileInfo(sFile);
            if (sfsc == true && firstFileInfo.Length < size * 1024)
            {
                firstFileInfo.CopyTo(dFile);
                return true;
            }
            Image iSource = Image.FromFile(sFile);
            ImageFormat tFormat = iSource.RawFormat;

            Bitmap ob = new Bitmap(iSource.Width, iSource.Height);

            Graphics g = Graphics.FromImage(ob);
            g.Clear(Color.White);
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(iSource, new Rectangle(0, 0, iSource.Width, iSource.Height), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
            g.Dispose();

            //設置壓縮質量
            EncoderParameters ep = new EncoderParameters();
            ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, new long[1] { flag });

            try
            {
                //找到系統中可用的圖片編碼器信息
                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegICIinfo = null;
                for (int x = 0; x < arrayICI.Length; x++)
                {
                    if (arrayICI[x].FormatDescription.Equals("JPEG"))
                    {
                        jpegICIinfo = arrayICI[x];
                        break;
                    }
                }
                //如果編碼器存在的,可以壓縮
                if (jpegICIinfo != null)
                {
                    ob.Save(dFile, jpegICIinfo, ep);
                    FileInfo fi = new FileInfo(dFile);
                    if (fi.Length > 1024 * size)
                    {
                        flag -= 10;
                        CompressImage(sFile, dFile, flag, size, false);
                    }
                }
                else
                {
                    ob.Save(dFile, tFormat);
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                iSource.Dispose();
                ob.Dispose();
            }
        }

調試代碼:

string spath = @"G:\Backup\pq1.jpg";
string dpath = @"G:\Backup\pq3.jpg";
CompressImage(spath,dpath);

結果:壓縮前(1.52 MB)

壓縮后(268 KB)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM