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
| public class Imager
{
///
/// 缩略模式。
///
public enum ThumnalMode : byte
{
///
/// 缩图模式
///
Full,
///
/// 切图模式
///
CUT,
}
///
/// 优化过后的图片保存功能,JPG 效果更好
///
/// 要保存的图片对象
/// 保存到文件
/// 质量 1-100
public static void Save(Bitmap bitmap, string fileName, int quality)
{
EncoderParameters encoderParam = new EncoderParameters();
encoderParam.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
System.Drawing.Imaging.ImageCodecInfo imageCodercInfo = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1];
bitmap.Save(fileName, imageCodercInfo, encoderParam);
}
///
/// 取得小图
///
/// 原图
/// 新的大小
/// 缩图的方式
/// mode=Full 无效 如果 mode=Max 的话,此参数表示切图的方式
///
public static Bitmap GetThumnail(Bitmap image, Size size, ThumnalMode mode, ContentAlignment contentAlignment)
{
if (!size.IsEmpty && !image.Size.IsEmpty && image.Width > size.Width)
{
//先取一个宽比例。
double scale = (double)image.Width / (double)size.Width;
Size newSize = size;
//缩略模式
switch (mode)
{
case ThumnalMode.Full:
{
scale = (double)image.Width / (double)size.Width;
newSize.Height = (int)(image.Height / scale);
break;
}
case ThumnalMode.CUT:
{
if (image.Height / scale < size.Height)
scale = (double)image.Height / (double)size.Height;
newSize.Width = (int)(image.Width / scale);
newSize.Height = (int)(image.Height / scale);
break;
}
}
Bitmap result;
if (mode == ThumnalMode.CUT)
{
result = new Bitmap(size.Width, size.Height);
}
else
{
result = new Bitmap(newSize.Width, newSize.Height);
}
using (Graphics g = Graphics.FromImage(result))
{
//背景颜色
g.FillRectangle(Brushes.White, new Rectangle(new Point(0, 0), size));
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingMode = CompositingMode.SourceOver;
g.CompositingQuality = CompositingQuality.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
//对齐方式
Rectangle destRect = new Rectangle(new Point(0, 0), newSize);
if (mode == ThumnalMode.CUT)
{
switch (contentAlignment)
{
case ContentAlignment.TopCenter:
destRect = new Rectangle(new Point(-((newSize.Width - size.Width) / 2), 0), newSize);
break;
case ContentAlignment.TopRight:
destRect = new Rectangle(new Point(-(newSize.Width - size.Width), 0), newSize);
break;
case ContentAlignment.MiddleLeft:
destRect = new Rectangle(new Point(0, -((newSize.Height - size.Height) / 2)), newSize);
break;
case ContentAlignment.MiddleCenter:
destRect = new Rectangle(new Point(-((newSize.Width - size.Width) / 2), -((newSize.Height - size.Height) / 2)), newSize);
break;
case ContentAlignment.MiddleRight:
destRect = new Rectangle(new Point(-(newSize.Width - size.Width), -((newSize.Height - size.Height) / 2)), newSize);
break;
case ContentAlignment.BottomLeft:
destRect = new Rectangle(new Point(0, -(newSize.Height - size.Height)), newSize);
break;
case ContentAlignment.BottomCenter:
destRect = new Rectangle(new Point(-((newSize.Width - size.Width) / 2), -(newSize.Height - size.Height)), newSize);
break;
case ContentAlignment.BottomRight:
destRect = new Rectangle(new Point(-(newSize.Width - size.Width), -(newSize.Height - size.Height)), newSize);
break;
default:
destRect = new Rectangle(new Point(0, 0), newSize);
break;
}
}
g.DrawImage(image, destRect, new Rectangle(new Point(0, 0), image.Size), GraphicsUnit.Pixel);
image.Dispose();
g.Dispose();
}
return result;
}
else
{
return image;
}
}
}
|