将图片大小无损压缩

希望对你有用。

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Drawing;
  4 using System.Drawing.Drawing2D;
  5 using System.Drawing.Imaging;
  6 using System.Linq;
  7 using System.Web;
  8 using System.Web.Mvc;
  9 
 10 namespace WebApplication1.Controllers
 11 {
 12     public class PressImgController : Controller
 13     {
 14         // GET: PressImg
 15         public ActionResult Index()
 16         {
 17             string img1 = "E:/娱乐/mmexport1536140834718.png";  //原图地址
 18             string img2 = "E:/娱乐/3211160.jpeg";  //图片保存新路径
 19             int height = 600;     //长度
 20             int weight = 300;     //宽度
 21             int flag = 60;        //压缩率
 22             GetPicThumbnail(img1, img2, height, weight, flag);
 23             return View();
 24         }
 25 
 26         /// 无损压缩图片  
 27         /// <param name="sFile">原图片</param>  
 28         /// <param name="dFile">压缩后保存位置</param>  
 29         /// <param name="dHeight">高度</param>  
 30         /// <param name="dWidth"></param>  
 31         /// <param name="flag">压缩质量(数字越小压缩率越高) 1-100</param>  
 32         /// <returns></returns>  
 33 
 34         public static bool GetPicThumbnail(string sFile, string dFile, int dHeight, int dWidth, int flag)
 35         {
 36             System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
 37             ImageFormat tFormat = iSource.RawFormat;
 38             int sW = 0, sH = 0;
 39 
 40             //按比例缩放
 41             Size tem_size = new Size(iSource.Width, iSource.Height);
 42 
 43             if (tem_size.Width > dHeight || tem_size.Width > dWidth)
 44             {
 45                 if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth))
 46                 {
 47                     sW = dWidth;
 48                     sH = (dWidth * tem_size.Height) / tem_size.Width;
 49                 }
 50                 else
 51                 {
 52                     sH = dHeight;
 53                     sW = (tem_size.Width * dHeight) / tem_size.Height;
 54                 }
 55             }
 56             else
 57             {
 58                 sW = tem_size.Width;
 59                 sH = tem_size.Height;
 60             }
 61 
 62             Bitmap ob = new Bitmap(dWidth, dHeight);
 63             Graphics g = Graphics.FromImage(ob);
 64 
 65             g.Clear(Color.WhiteSmoke);
 66             g.CompositingQuality = CompositingQuality.HighQuality;
 67             g.SmoothingMode = SmoothingMode.HighQuality;
 68             g.InterpolationMode = InterpolationMode.HighQualityBicubic;
 69 
 70             g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
 71 
 72             g.Dispose();
 73             //以下代码为保存图片时,设置压缩质量  
 74             EncoderParameters ep = new EncoderParameters();
 75             long[] qy = new long[1];
 76             qy[0] = flag;//设置压缩的比例1-100  
 77             EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
 78             ep.Param[0] = eParam;
 79             try
 80             {
 81                 ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
 82                 ImageCodecInfo jpegICIinfo = null;
 83                 for (int x = 0; x < arrayICI.Length; x++)
 84                 {
 85                     if (arrayICI[x].FormatDescription.Equals("JPEG"))
 86                     {
 87                         jpegICIinfo = arrayICI[x];
 88                         break;
 89                     }
 90                 }
 91                 if (jpegICIinfo != null)
 92                 {
 93                     ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径  
 94                 }
 95                 else
 96                 {
 97                     ob.Save(dFile, tFormat);
 98                 }
 99                 return true;
100             }
101             catch
102             {
103                 return false;
104             }
105             finally
106             {
107                 iSource.Dispose();
108                 ob.Dispose();
109             }
110         }
111 
112     }
113 }

 

 posted on 2018-09-05 18:16  金箍棒哒哒哒  阅读(245)  评论(0)    收藏  举报