C# 图片无损压缩

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         }
复制代码
复制代码
作者:Peter Luo
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。

posted on   漫思  阅读(11)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
历史上的今天:
2023-12-31 优雅的设计
2023-12-31 BBC 开源的声音可视化库
2019-12-31 React.Children的使用
2019-12-31 如何更改账户信息

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示