wyb
2021-05-11 49ce087bd2a34a150597e1cc1da157af242c0b6d
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Pcb.Common.Utilities
{
    public class ImageUtility
    {
        public static void CompressionAndSaveImage(System.Web.HttpPostedFile file, string savePath, int width = 640, int height = 360, int Level = 80, bool CompressionAlways = false)
        {
            if (Level < 0 || Level > 100)
                throw new Exception("压缩率必须在0-100之间");
            // Get a bitmap.
            if (file.ContentLength <= 30 * 1024 && !CompressionAlways)
            {
                file.SaveAs(savePath);
                return;
            }
 
            Image bmpOld = Image.FromStream(file.InputStream);
            Bitmap img = new Bitmap(bmpOld);
            Bitmap bmp2 = null;
            bmpOld.Dispose();
 
            if (file.ContentLength > 100 * 1024 || CompressionAlways)
            {
 
                int srcWidth = img.Width;
                int srcHeight = img.Height;
 
                int thumbWidth = width;
                int thumbHeight = height;
 
                ImageWH iwh = GetImageNewWH(srcWidth, srcHeight, width, height);
                thumbWidth = iwh.width;
                thumbHeight = iwh.height;
 
                if (thumbWidth > width || thumbHeight > height)
                {
                    iwh = GetImageNewWH(thumbWidth, thumbHeight, width, height);
                    thumbWidth = iwh.width;
                    thumbHeight = iwh.height;
                }
 
                Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
 
                //从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。
                System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
 
                //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality
                gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //gr.SmoothingMode = SmoothingMode.None;
 
                ////下面这个也设成高质量
                gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                //gr.CompositingQuality = CompositingQuality.HighSpeed;
 
                ////下面这个设成High
                gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                //gr.InterpolationMode = InterpolationMode.Low;
 
                gr.Clear(System.Drawing.Color.Transparent);
 
                img.SetResolution(72, 72);
 
                //把原始图像绘制成上面所设置宽高的缩小图
                System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
                gr.DrawImage(img, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
                bmp2 = new Bitmap(bmp);
                bmp.Dispose();
                gr.Dispose();
            }
            else
                bmp2 = img;
 
            ImageCodecInfo Encoder = null;
            Encoder = GetEncoder(ImageFormat.Jpeg);
            // Create an Encoder object based on the GUID
            // for the Quality parameter category.
            System.Drawing.Imaging.Encoder myEncoder =
                System.Drawing.Imaging.Encoder.Quality;
 
            // Create an EncoderParameters object.
            // An EncoderParameters object has an array of EncoderParameter
            // objects. In this case, there is only one
            // EncoderParameter object in the array.
            EncoderParameters EncoderParameters = new EncoderParameters(1);
 
            EncoderParameter EncoderParameter = new EncoderParameter(myEncoder, Level);
            EncoderParameters.Param[0] = EncoderParameter;
            bmp2.Save(savePath, Encoder, EncoderParameters);
 
            bmp2.Dispose();
            img.Dispose();
        }
 
 
        private static ImageCodecInfo GetEncoder(ImageFormat format)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                    return codec;
            }
            return null;
        }
 
        private static ImageWH GetImageNewWH(int srcWidth, int srcHeight, int width, int height)
        {
            ImageWH iwh = new ImageWH();
 
            if (srcWidth > width && srcHeight > height)
            {
                if (srcWidth > srcHeight)
                {
                    double heightFloat = Convert.ToDouble(srcHeight) / Convert.ToDouble(srcWidth);
                    heightFloat = heightFloat * width;
                    iwh.height = Convert.ToInt32(heightFloat);
                    iwh.width = width;
                }
                else
                {
                    double widthFloat = Convert.ToDouble(srcWidth) / Convert.ToDouble(srcHeight);
                    widthFloat = widthFloat * height;
                    iwh.width = Convert.ToInt32(widthFloat);
                    iwh.height = height;
                }
            }
            else if (srcWidth > width)
            {
                double heightFloat = Convert.ToDouble(srcHeight) / Convert.ToDouble(srcWidth);
                heightFloat = heightFloat * width;
                iwh.height = Convert.ToInt32(heightFloat);
                iwh.width = width;
            }
            else if (srcHeight > height)
            {
                double widthFloat = Convert.ToDouble(srcWidth) / Convert.ToDouble(srcHeight);
                widthFloat = widthFloat * height;
                iwh.width = Convert.ToInt32(widthFloat);
                iwh.height = height;
            }
            else
            {
                iwh.width = srcWidth;
                iwh.height = srcHeight;
            }
 
            return iwh;
        }
 
        private class ImageWH
        {
            public int width { get; set; }
 
            public int height { get; set; }
        }
    }
}