C# 无损压缩图片

View Code
 1 //命名空间
 2 using System.Drawing.Imaging;
 3 using System.Drawing;
 4 using System.Drawing.Drawing2D;
 5 
 6    /// <summary>
 7         /// 无损压缩图片
 8         /// </summary>
 9         /// <param name="sFile">原图片</param>
10         /// <param name="dFile">压缩后保存位置</param>
11         /// <param name="dHeight">高度</param>
12         /// <param name="dWidth"></param>
13         /// <param name="flag">压缩质量 1-100</param>
14         /// <returns></returns>
15         public static bool GetPicThumbnail(string sFile, string dFile, int dHeight, int dWidth, int flag)
16         {
17 
18             System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
19             ImageFormat tFormat = iSource.RawFormat;
20             int sW = 0, sH = 0;
21             //按比例缩放
22             Size tem_size = new Size(iSource.Width, iSource.Height);
23 
24             if (tem_size.Width > dHeight||tem_size.Width > dWidth) 
25             {
26                 if ((tem_size.Width * dHeight) > (tem_size.Height * dWidth))
27                 {
28                     sW = dWidth;
29                     sH = (dWidth * tem_size.Height) / tem_size.Width;
30                 }
31                 else
32                 {
33                     sH = dHeight;
34                     sW = (tem_size.Width * dHeight) / tem_size.Height;
35                 }
36             }
37             else
38             {
39                 sW = tem_size.Width;
40                 sH = tem_size.Height;
41             }
42             Bitmap ob = new Bitmap(dWidth, dHeight);
43             Graphics g = Graphics.FromImage(ob);
44             g.Clear(Color.WhiteSmoke);
45             g.CompositingQuality = CompositingQuality.HighQuality;
46             g.SmoothingMode = SmoothingMode.HighQuality;
47             g.InterpolationMode = InterpolationMode.HighQualityBicubic;
48             g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
49             g.Dispose();
50             //以下代码为保存图片时,设置压缩质量
51             EncoderParameters ep = new EncoderParameters();
52             long[] qy = new long[1];
53             qy[0] = flag;//设置压缩的比例1-100
54             EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
55             ep.Param[0] = eParam;
56             try
57             {
58                 ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
59                 ImageCodecInfo jpegICIinfo = null;
60                 for (int x = 0; x < arrayICI.Length; x++)
61                 {
62                     if (arrayICI[x].FormatDescription.Equals("JPEG"))
63                     {
64                         jpegICIinfo = arrayICI[x];
65                         break;
66                     }
67                 }
68                 if (jpegICIinfo != null)
69                 {
70                     ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
71                 }
72                 else
73                 {
74                     ob.Save(dFile, tFormat);
75                 }
76                 return true;
77             }
78             catch
79             {
80                 return false;
81             }
82             finally
83             {
84                 iSource.Dispose();
85                 ob.Dispose();
86             }
87 
88         }

来至网摘

posted @ 2012-05-12 13:12  ミ茹此茹此↘  Views(416)  Comments(0Edit  收藏  举报