随笔 - 83,  文章 - 6,  评论 - 20,  阅读 - 10万

小程序建议图片大小限制在200kb以内,故而需要将图片的大小无损压缩到200kb以内。

用C#写了下代码实现图片的压缩(你可以设置需要压缩的图片的大小)

 1. 前端代码 Index.aspx

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
29
30
31
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebApplication5.Index" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>原始图片:</td>
                    <td>
                        <asp:Image ID="Image1" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>压缩图片:</td>
                    <td>
                        <asp:Image ID="Image2" runat="server" />
                    </td>
                </tr>
            </table>
        </div>
         
    </form>
</body>
</html>

2. 后端代码 Index.aspx.cs

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
 
namespace WebApplication5
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string sourceFileName = "details_01.png";
                Image1.ImageUrl = sourceFileName;
                string sourcePath = Server.MapPath("~/") + sourceFileName;//图片原路径
                //获取后缀
                string extname = Path.GetExtension(sourcePath);//获取文件的拓展名称
                string newFileName = Guid.NewGuid().ToString().Substring(0, 6) + extname;
                string newPath = Server.MapPath("~/") + newFileName;
 
                CompressImage(sourcePath, newPath);
 
                Image2.ImageUrl = newFileName;
            }
        }
 
        /// <summary>
        /// 无损压缩图片(默认为 不超过200kb)
        /// </summary>
        /// <param name="sourceFile">原图片地址</param>
        /// <param name="newFile">压缩后保存图片地址</param>
        /// <param name="size">压缩后图片的最大大小 默认为 不超过200kb</param>
        /// <returns></returns>
        public void CompressImage(string sourceFile, string newFile, int size = 200)
        {
            //设置大小偏差幅度 1kb
            const long nearlyLen = 1024;
 
            //如果是第一次调用,原始图像的大小小于要压缩的大小,则直接复制文件,并且返回true
            FileInfo firstFileInfo = new FileInfo(sourceFile);
            long sourceLen = firstFileInfo.Length; //原始大小
            long targetLen = size * 1024; //压缩后大小
            if (sourceLen <= targetLen)
            {
                firstFileInfo.CopyTo(newFile);
            }
            else
            {
                //获取目标大小最低值
                var exitLen = targetLen - nearlyLen;
 
                using (System.Drawing.Image iSource = System.Drawing.Image.FromFile(sourceFile))
                {
                    ImageFormat sourceFormat = iSource.RawFormat; //图片格式
                    int sourceHeight = iSource.Height;
                    int sourceWidth = iSource.Width;
                    int sW = 0, sH = 0;
                    //按比例缩放
                    Size tem_size = new Size(sourceWidth, sourceHeight);
                    if (tem_size.Width > sourceHeight || tem_size.Width > sourceWidth)
                    {
                        if ((tem_size.Width * sourceHeight) > (tem_size.Width * sourceWidth))
                        {
                            sW = sourceWidth; sH = (sourceWidth * tem_size.Height) / tem_size.Width;
                        }
                        else
                        {
                            sH = sourceHeight;
                            sW = (tem_size.Width * sourceHeight) / tem_size.Height;
                        }
                    }
                    else
                    {
                        sW = tem_size.Width;
                        sH = tem_size.Height;
                    }
                    using (Bitmap ob = new Bitmap(sourceWidth, sourceHeight))
                    {
                        using (Graphics g = Graphics.FromImage(ob))
                        {
                            g.Clear(Color.WhiteSmoke);
                            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                            g.DrawImage(iSource, new Rectangle((sourceWidth - sW) / 2, (sourceHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
                        }
                         
                        //获取编码器信息
                        var format = ImageFormat.Jpeg;
                        ImageCodecInfo formatInfo = GetEncoder(format);
                        if (formatInfo != null)
                        {
                            //以下代码为保存图片时,设置压缩质量
                            var parms = new EncoderParameters(1);
                            //初始化质量压缩参数 压缩质量(数字越小压缩率越高)1-100
                            var quality = (long)Math.Floor(100.00 * targetLen / sourceLen);
 
                            //使用二分法进行查找 最接近的质量参数
                            long startQuality = quality;
                            long endQuality = 100;
 
                            using (var ms = new MemoryStream())
                            {
                                while (true)
                                {
                                    parms.Param[0] = new EncoderParameter(Encoder.Quality, quality);
 
                                    ms.SetLength(0);
                                    ms.Position = 0;
                                    ob.Save(ms, formatInfo, parms);
 
                                    long newImageLen = ms.Length;
                                    //若压缩后大小低于目标大小,则满足条件退出
                                    if (newImageLen >= exitLen && newImageLen <= targetLen)
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        //区间相等无需再次计算
                                        if (startQuality >= endQuality)
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            if (newImageLen < exitLen) //压缩过小,起始质量右移
                                            {
                                                startQuality = quality;
                                            }
                                            else //压缩过大 终止质量左移
                                            {
                                                endQuality = quality;
                                            }
 
                                            //重新设置质量参数 如果计算出来的质量没有发生变化,则终止查找。这样是为了避免重复计算情况{start:16,end:18} 和 {start:16,endQuality:17}
                                            long newQuality = (startQuality + endQuality) / 2;
                                            if (newQuality == quality)
                                            {
                                                break;
                                            }
                                            else
                                            {
                                                quality = newQuality;
                                            }
                                        }
                                    }
                                }
                            }
                            ob.Save(newFile, formatInfo, parms);//dFile是压缩后的新路径
                        }
                        else
                        {
                            ob.Save(newFile, sourceFormat);
                        }
                    }
                }
            }
        }
 
        /// <summary>
        /// 获取编码器信息
        /// </summary>
        /// <param name="format"></param>
        /// <returns></returns>
        private ImageCodecInfo GetEncoder(ImageFormat format)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }
    }
}

运行项目既可以看到压缩后的图片。

代码经过运行测试。

posted on   £冷☆月№  阅读(102)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

< 2025年3月 >
23 24 25 26 27 28 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 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示