C# 压缩图片到指定宽度,假如图片小于指定宽度 判断图片大小是否大于指定大小(KB) 如果大于则压缩图片质量 宽高不变

2021-07-08 16:08

阅读:648

标签:source   ubi   分享图片   技术   ng2   压缩图片   oid   src   coder   

技术分享图片技术分享图片
class Program
{
    static void Main(string[] args)
    {//G:\zhyue\backup\projects\Test\ConsoleApplication1\img
     //var url = "http://seo.jrechina.com/houselist/";
     //var res = WebRequestExt.GetData(url);

        string img_url = @"D:\Documents\Pictures\壁纸\179 KB.jpg";
        string img_savePath = @"G:\zhyue\backup\projects\Test\ConsoleApplication1\img\179 KB.jpg";

        string[] files=Directory.GetFiles(@"D:\Documents\Pictures\准备压缩图片");
        foreach (var file in files)
        {
            img_url = file;
            img_savePath = @"G:\zhyue\backup\projects\Test\ConsoleApplication1\img\" + Path.GetFileName(img_url);
            bool res = ImgHelper.CompressImageWidthTo760(img_url, img_savePath, 120, 200, true);
        }

        Console.WriteLine("ok");
        Console.ReadKey();
    }
}

lic class ImgHelper
{
    /// 
    /// 无损压缩图片 压缩宽度到指定宽度760 小于指定宽度的判断size是否大于200KB进行质量压缩 宽高不变
    /// 
    /// 原图片地址
    /// 压缩后保存图片地址
    /// 压缩质量(数字越小压缩率越高)1-100
    /// 压缩后图片的最大大小 KB
    /// 是否是第一次调用
    /// 
    public static bool CompressImageWidthTo760(string sFile, string dFile, int flag = 90, int size = 1000, bool sfsc = true)
    {
        using (Image iSource = Image.FromFile(sFile))
        {
            ImageFormat tFormat = iSource.RawFormat;
            //如果是第一次调用,原始图像的大小小于要压缩的大小,则直接复制文件,并且返回true
            FileInfo firstFileInfo = new FileInfo(sFile);
            if (sfsc == true && firstFileInfo.Length 1024 && iSource.Width 760)
            {
                firstFileInfo.CopyTo(dFile, true);
                return true;
            }

            if (firstFileInfo.Length > size * 1024 && iSource.Width 760)
            {//超过200KB只压缩质量不压缩宽高
                return NewMethodByQuality(sFile, dFile, ref flag, size, tFormat, iSource as Bitmap);
            }

            //每次压缩一半的宽高比例
            double percent = 760.0 / iSource.Width;
            int dHeight = (int)Math.Ceiling(percent * iSource.Height);
            int dWidth = 760;

            int sW = 0, sH = 0;
            //按比例缩放
            Size tem_size = new Size(iSource.Width, iSource.Height);
            if (tem_size.Width > dHeight || tem_size.Width > dWidth)
            {
                if ((tem_size.Width * dHeight) > (tem_size.Width * 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;
            }

            using (Bitmap ob = new Bitmap(dWidth, dHeight))
            {
                using (Graphics g = Graphics.FromImage(ob))
                {
                    g.Clear(Color.WhiteSmoke);
                    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((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
                    //g.DrawImage(iSource, new Rectangle((int)Math.Ceiling((dWidth - sW) / percent), (int)Math.Ceiling((dHeight - sH) / percent), sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
                    g.DrawImage(iSource, new Rectangle((int)Math.Ceiling((dWidth - sW) / percent), (int)Math.Ceiling((dHeight - sH) / percent), sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);

                    g.Dispose();
                }

                return NewMethodByQuality(sFile, dFile, ref flag, size, tFormat, ob);
            }
        }
    }
    /// 
    /// 根据图片质量压缩
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    private static bool NewMethodByQuality(string sFile, string dFile, ref int flag, int size, ImageFormat tFormat, Bitmap ob)
    {
        //以下代码为保存图片时,设置压缩质量
        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 )
            {
                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 = flag - 10;
                    CompressImageWidthTo760(sFile, dFile, flag, size, false);
                }
            }
            else
            {
                ob.Save(dFile, tFormat);
            }
            return true;
        }
        catch
        {
            return false;
        }
    }
}
图片等比例压缩

说明:等比例压缩图片到指定宽度,假如图片小于指定宽度 判断图片大小是否大于指定大小(KB) 如果大于则压缩图片质量 宽高不变

技术分享图片

压缩后:

技术分享图片

 

C# 压缩图片到指定宽度,假如图片小于指定宽度 判断图片大小是否大于指定大小(KB) 如果大于则压缩图片质量 宽高不变

标签:source   ubi   分享图片   技术   ng2   压缩图片   oid   src   coder   

原文地址:https://www.cnblogs.com/zhyue93/p/image_compress.html


评论


亲,登录后才可以留言!