C# 递归压缩图片

整理压缩代码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace CompressImg
{
    public class CompressHelp
    {
    
     /// <summary>
        /// 无损压缩图片
        /// </summary>
        /// <param name="sFile">文件位置</param>
        /// <param name="dFile">压缩后保存位置</param>
        /// <param name="flag">压缩比例,eg:按照百分比算如80,就是原图的高度乘以80%</param>
        /// <returns></returns>
        public  bool GetPicThumbnail(string sFile, string dFile, int flag)
        {
            int size = 200;//设置图片压缩大小值以内
            System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
            ImageFormat tFormat = iSource.RawFormat;
            int sW = 0, sH = 0;
            FileInfo dFiles = new FileInfo(dFile);
            //判断目录是否存在
            if (!Directory.Exists(dFiles.DirectoryName))
            {
                Directory.CreateDirectory(dFiles.DirectoryName);
            }
            FileInfo fileInfo = new FileInfo(sFile);
            //判断图片大小
            if (fileInfo.Length <= 1024 * size)
            {   
                fileInfo.CopyTo(dFile, true);
                return true;
            }

            //按比例缩放
            Size tem_size = new Size(iSource.Width, iSource.Height);
            int dHeight = iSource.Height * flag / 100; //高度压缩百分不
            int dWidth = iSource.Width * flag / 100;//宽度压缩百分比
            if (tem_size.Width > dHeight || tem_size.Width > dWidth) //将**改成c#中的或者操作符号
            {
                if ((tem_size.Width * dHeight) > (tem_size.Height * dWidth))
                {
                    sW = dWidth;
                    sH = (dWidth * tem_size.Height) / tem_size.Width;
                }
                else
                {
                    sH = dHeight;
                    sW = (tem_size.Width * dHeight) / tem_size.Height;
                }
            }
            else
            {
                sW = tem_size.Width;
                sH = tem_size.Height;
            }

            Bitmap ob = new Bitmap(dWidth, dHeight);
            Graphics g = Graphics.FromImage(ob);
            g.Clear(Color.WhiteSmoke);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
            g.Dispose();
            //以下代码为保存图片时,设置压缩质量
            EncoderParameters ep = new EncoderParameters();
            long[] qy = new long[1];
            qy[0] = flag;//设置压缩的比例1-100
            EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
            ep.Param[0] = eParam;
            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);//dFile是压缩后的新路径
                    FileInfo fi = new FileInfo(dFile);
                    if (fi.Length > 1024 * size)
                    {
                        flag -= 10;
                        GetPicThumbnail(sFile, dFile, flag);
                    } 
                }
                else
                {
                    ob.Save(dFile, tFormat);
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                iSource.Dispose();
                ob.Dispose();

            }
        }

    }
}

 

调用:

CompressHelp compressHelp = new CompressHelp();

 compressHelp.GetPicThumbnail("G:\1\1.jpg", "F:\1\1.jpg", 50);

 

上一篇:vue_内部私有组件


下一篇:数据可视化基础专题(二十):Pandas120题(五)81-100