【代码】C#实现图片压缩

2022-08-04 16:01:26  阅读 3336 次 评论 1 条

项目需求,要把一些图片本地化,容量还不能占用太高,唉。。。

直接上代码

 public class ImageUtil
    {
        /// <summary>
        /// 降低图片分辨率,按比例(1~100),比例失效 则按自定义长宽
        /// </summary>
        /// <param name="image"></param>
        /// <param name="ratePic"></param>
        /// <param name="widthPic"></param>
        /// <param name="heightPic"></param>
        /// <returns></returns>
        public static Image LowPicPoint(Image image, int ratePic, int widthPic, int heightPic)
        {
            Graphics grap = null;
            try
            {
                int width, height;
                if (1 <= ratePic && ratePic <= 100)
                {
                    width = Convert.ToInt32(image.Width * (Convert.ToDouble(ratePic) / 100));
                    height = Convert.ToInt32(image.Height * (Convert.ToDouble(ratePic) / 100));
                }
                else
                {
                    width = widthPic;
                    height = heightPic;
                }
                Image newImage = new Bitmap(width, height);
                grap = Graphics.FromImage(newImage);
                grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                grap.DrawImage(image, new Rectangle(0, 0, width, height));
                return (newImage);
            }
            catch (Exception ex)
            {
                throw new Exception("降低图片分辨率失效!", ex);
            }
            finally
            {
                if (grap != null) grap.Dispose();
            }
        }
 
 
        /// 无损压缩图片    
        /// <param name="sFile">原图片</param>    
        /// <param name="dFile">压缩后保存位置</param>    
        /// <param name="dHeight">高度</param>    
        /// <param name="dWidth"></param>    
        /// <param name="flag">压缩质量(数字越小压缩率越高) 1-100</param>    
        /// <returns></returns>    
 
        public static bool GetPicThumbnail(string sFile, string dFile, int flag, int dWidth, int dHeight)
        {
            System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
            ImageFormat tFormat = iSource.RawFormat;
            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;
            }
 
            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(0, 0, dWidth, dHeight), 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)
                {
                    iSource.Dispose();
                   
                    ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径    
                }
                else
                {
                    iSource.Dispose();
                    
                    ob.Save(dFile, tFormat);
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                iSource.Dispose();
                ob.Dispose();
            }
        }
    }



微信扫码查看本文
本文地址:https://www.yangguangdream.com/?id=2213
版权声明:本文为原创文章,版权归 编辑君 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

评论列表

  1. APP用户
    APP用户  @回复

    o(*≧▽≦)ツ超赞