zoukankan      html  css  js  c++  java
  • 高质量图片无损压缩算法

    朋友开了网店,想让我帮忙做一些小工具,其中就包含了图片无损压缩的需求,这一类的软件和代码在网上有很多,但是真正实用的符合自己定制化需求的没几个,于是就想着自己DIY一下(自己动手丰衣足食^o^),并贴出核心代码供大家学习和参考

            /// <summary>
            /// 无损压缩图片
            /// </summary>
            /// <param name="sFile">原图片地址</param>
            /// <param name="dFile">压缩后保存图片地址</param>
            /// <param name="flag">压缩质量(数字越小压缩率越高)1-100</param>
            /// <returns></returns>
            public static bool CompressImage(string sFile, string dFile, int flag = 90)
            {
                Image iSource = Image.FromFile(sFile);
                ImageFormat tFormat = iSource.RawFormat;
    
                int dHeight = iSource.Height / 2;
                int dWidth = iSource.Width / 2;
                int sW, sH;
                //按比例缩放
                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;
                }
    
                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(Encoder.Quality, qy);
                ep.Param[0] = eParam;
    
                try
                {
                    ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                    ImageCodecInfo jpegICIinfo = arrayICI.FirstOrDefault(t => t.FormatDescription.Equals("JPEG"));
                    if (jpegICIinfo != null)
                    {
                        ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
                    }
                    else
                    {
                        ob.Save(dFile, tFormat);
                    }
                    return true;
                }
                catch
                {
                    return false;
                }
                finally
                {
                    iSource.Dispose();
                    ob.Dispose();
                }
            }
  • 相关阅读:
    HTML转义字符
    网站项目开发经验总结
    SQL SERVER事务处理
    5种方法在mac系统修改hosts文件
    用MPMoviePlayerController做在线音乐播放
    UISearchBar的使用心得
    10 个免费的 C/C++ 集成开发环境
    TableView有些属性的修改只能在init里面
    UIScrollView的正确使用方法
    设置UIView阴影shadow 边框 边框颜色
  • 原文地址:https://www.cnblogs.com/guyun/p/8027797.html
Copyright © 2011-2022 走看看