Browse Source

Merge branch 'master' of https://github.com/ldqk/Masuit.Tools

懒得勤快 7 years ago
parent
commit
ac6e893ab6
1 changed files with 82 additions and 42 deletions
  1. 82 42
      Masuit.Tools/Media/ImageUtilities.cs

+ 82 - 42
Masuit.Tools/Media/ImageUtilities.cs

@@ -555,55 +555,95 @@ namespace Masuit.Tools.Media
         /// <summary>
         /// 无损压缩图片
         /// </summary>
-        /// <param name="image">原图片</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(this Image image, string dFile, int dHeight, int dWidth, int flag = 100)
+        /// <param name="sFile">原图片地址</param>
+        /// <param name="dFile">压缩后保存图片地址</param>
+        /// <param name="flag">压缩质量(数字越小压缩率越高)1-100</param>
+        /// <param name="size">压缩后图片的最大大小</param>
+        /// <param name="sfsc">是否是第一次调用</param>
+        /// <returns></returns>
+        public static bool CompressImage(string sFile, string dFile, int flag = 90, int size = 1024, bool sfsc = true)
         {
-            ImageFormat tFormat = image.RawFormat;
-            int sW, sH;
-            //按比例缩放
-            Size temSize = new Size(image.Width, image.Height);
-            if ((temSize.Width > dHeight) || (temSize.Width > dWidth)) //将**改成c#中的或者操作符号
+            //如果是第一次调用,原始图像的大小小于要压缩的大小,则直接复制文件,并且返回true
+            FileInfo firstFileInfo = new FileInfo(sFile);
+            if (sfsc && firstFileInfo.Length < size * 1024)
+            {
+                firstFileInfo.CopyTo(dFile);
+                return true;
+            }
+
+            using (Image iSource = Image.FromFile(sFile))
             {
-                if (temSize.Width * dHeight > temSize.Height * dWidth)
+                ImageFormat tFormat = iSource.RawFormat;
+                int dHeight = iSource.Height;
+                int dWidth = iSource.Width;
+                int sW, sH;
+                //按比例缩放
+                Size temSize = new Size(iSource.Width, iSource.Height);
+                if (temSize.Width > dHeight || temSize.Width > dWidth)
                 {
-                    sW = dWidth;
-                    sH = dWidth * temSize.Height / temSize.Width;
+                    if ((temSize.Width * dHeight) > (temSize.Width * dWidth))
+                    {
+                        sW = dWidth;
+                        sH = (dWidth * temSize.Height) / temSize.Width;
+                    }
+                    else
+                    {
+                        sH = dHeight;
+                        sW = (temSize.Width * dHeight) / temSize.Height;
+                    }
                 }
                 else
                 {
-                    sH = dHeight;
-                    sW = temSize.Width * dHeight / temSize.Height;
+                    sW = temSize.Width;
+                    sH = temSize.Height;
+                }
+
+                using (Bitmap bmp = new Bitmap(dWidth, dHeight))
+                {
+                    using (Graphics g = Graphics.FromImage(bmp))
+                    {
+                        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);
+                    }
+
+                    //以下代码为保存图片时,设置压缩质量
+                    using (EncoderParameters ep = new EncoderParameters())
+                    {
+                        long[] qy = new long[1];
+                        qy[0] = flag;//设置压缩的比例1-100
+                        using (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)
+                                {
+                                    bmp.Save(dFile, jpegIcIinfo, ep);//dFile是压缩后的新路径
+                                    FileInfo fi = new FileInfo(dFile);
+                                    if (fi.Length > 1024 * size)
+                                    {
+                                        flag = flag - 10;
+                                        CompressImage(sFile, dFile, flag, size, false);
+                                    }
+                                }
+                                else
+                                {
+                                    bmp.Save(dFile, tFormat);
+                                }
+                                return true;
+                            }
+                            catch
+                            {
+                                return false;
+                            }
+                        }
+                    }
                 }
-            }
-            else
-            {
-                sW = temSize.Width;
-                sH = temSize.Height;
-            }
-            using (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(image, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
-                g.Dispose();
-                //以下代码为保存图片时,设置压缩质量
-                var ep = new EncoderParameters();
-                long[] qy = new long[1];
-                qy[0] = flag; //设置压缩的比例1-100
-                ep.Param[0] = new EncoderParameter(Encoder.Quality, qy);
-                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;
             }
         }