C# 图片无损压缩

这两天忙于将扫描仪扫描出来的文件缩放图片分辨率和大小,开始尝试修改扫描仪设置的参数,结果发现没有办法修改扫描仪参数,最后尝试将扫描后的图片进行代码处理,下面是写的是关于图片进行无损压缩的代码。

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