今天想学习一下图像变形的技巧,无意间在网上看到这个代码,觉得不错,转发一下:
1
using System;
2
using System.Collections.Generic;
3
using System.Drawing;
4
using System.Drawing.Drawing2D;
5
using System.Drawing.Imaging;
6
namespace Steam.Core
7
{
8
public static class ImageDeal
9
{
10
/**//// <summary>
11
/// 缩略图,按高度和宽度来缩略
12
///http://www.cnblogs.com/pooeo/
13
/// </summary>
14
/// <param name="image"></param>
15
/// <param name="size"></param>
16
/// <returns></returns>
17
public static Image Scale(Image image, Size size)
18
{
19
return image.GetThumbnailImage(size.Width, size.Height, null, new IntPtr());
20
}
21
/**//// <summary>
22
/// 缩略图,按倍数来缩略
23
///http://www.cnblogs.com/pooeo/
24
/// </summary>
25
/// <param name="image">原图</param>
26
/// <param name="multiple">放大或缩小的倍数,负数表示缩小,正数表示放大</param>
27
/// <returns></returns>
28
public static Image Scale(Image image, Int32 multiple)
29
{
30
Int32 newWidth;
31
Int32 newHeight;
32
Int32 absMultiple = Math.Abs(multiple);
33
if (multiple == 0)
34
{
35
return image.Clone() as Image;
36
}
37
if (multiple < 0)
38
{
39
newWidth = image.Width / absMultiple;
40
newHeight = image.Height / absMultiple;
41
}
42
else
43
{
44
newWidth = image.Width * absMultiple;
45
newHeight = image.Height * absMultiple;
46
}
47
return image.GetThumbnailImage(newWidth, newHeight, null, new IntPtr());
48
}
49
/**//// <summary>
50
/// 固定宽度缩略
51
///http://www.cnblogs.com/pooeo/
52
/// </summary>
53
/// <param name="image"></param>
54
/// <param name="width"></param>
55
/// <returns></returns>
56
public static Image ScaleFixWidth(Image image, Int32 width)
57
{
58
Int32 newWidth = width;
59
Int32 newHeight;
60
Double tempMultiple = (Double)newWidth / (Double)image.Width;
61
newHeight = (Int32)(((Double)image.Height) * tempMultiple);
62
Image newImage = new Bitmap(newWidth, newHeight);
63
using (Graphics newGp = Graphics.FromImage(newImage))
64
{
65
newGp.CompositingQuality = CompositingQuality.HighQuality;
66
//设置高质量插值法
67
newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
68
//设置高质量,低速度呈现平滑程度
69
newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
70
//清空画布并以透明背景色填充
71
newGp.Clear(Color.Transparent);
72
newGp.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight));
73
}
74
return newImage;
75
}
76
/**//// <summary>
77
/// 固定高度缩略
78
///http://www.cnblogs.com/pooeo/
79
/// </summary>
80
/// <param name="image"></param>
81
/// <param name="height"></param>
82
/// <returns></returns>
83
public static Image ScaleFixHeight(Image image, Int32 height)
84
{
85
Int32 newWidth;
86
Int32 newHeight = height;
87
Double tempMultiple = (Double)newHeight / (Double)image.Height;
88
newWidth = (Int32)(((Double)image.Width) * tempMultiple);
89
Image newImage = new Bitmap(newWidth, newHeight);
90
using (Graphics newGp = Graphics.FromImage(newImage))
91
{
92
newGp.CompositingQuality = CompositingQuality.HighQuality;
93
//设置高质量插值法
94
newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
95
//设置高质量,低速度呈现平滑程度
96
newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
97
//清空画布并以透明背景色填充
98
newGp.Clear(Color.Transparent);
99
newGp.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight));
100
}
101
return newImage;
102
}
103
/**//// <summary>
104
/// 裁减缩略,根据固定的高度和宽度
105
///http://www.cnblogs.com/pooeo/
106
/// </summary>
107
/// <param name="image"></param>
108
/// <param name="width"></param>
109
/// <param name="heigth"></param>
110
/// <returns></returns>
111
public static Image ScaleCut(Image image, Int32 width, Int32 height)
112
{
113
int x = 0;
114
int y = 0;
115
int ow = image.Width;
116
int oh = image.Height;
117
if (width >= ow && height >= oh)
118
{
119
return image;
120
}
121
//如果结果要比原来的宽
122
if (width > ow)
123
{
124
width = ow;
125
}
126
if (height > oh)
127
{
128
height = oh;
129
}
130
if ((double)image.Width / (double)image.Height > (double)width / (double)height)
131
{
132
oh = image.Height;
133
ow = image.Height * width / height;
134
y = 0;
135
x = (image.Width - ow) / 2;
136
}
137
else
138
{
139
ow = image.Width;
140
oh = image.Width * height / width;
141
x = 0;
142
y = (image.Height - oh) / 2;
143
}
144
Image newImage = new Bitmap(width, height);
145
using (Graphics newGp = Graphics.FromImage(newImage))
146
{
147
newGp.CompositingQuality = CompositingQuality.HighQuality;
148
//设置高质量插值法
149
newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
150
//设置高质量,低速度呈现平滑程度
151
newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
152
//清空画布并以透明背景色填充
153
newGp.Clear(Color.Transparent);
154
newGp.DrawImage(image, new Rectangle(0, 0, width, height),
155
new Rectangle(x, y, ow, oh),
156
GraphicsUnit.Pixel);
157
}
158
return newImage;
159
}
160
/**//// <summary>
161
/// 生成缩略图
162
///http://www.cnblogs.com/pooeo/
163
/// </summary>
164
/// <param name="originalImagePath">源图路径(物理路径)</param>
165
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
166
/// <param name="width">缩略图宽度</param>
167
/// <param name="height">缩略图高度</param>
168
/// <param name="mode">生成缩略图的方式</param>
169
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
170
{
171
Image originalImage = Image.FromFile(originalImagePath);
172
int towidth = width;
173
int toheight = height;
174
int x = 0;
175
int y = 0;
176
int ow = originalImage.Width;
177
int oh = originalImage.Height;
178
switch (mode)
179
{
180
case "HW"://指定高宽缩放(可能变形)
181
break;
182
case "W"://指定宽,高按比例
183
toheight = originalImage.Height * width / originalImage.Width;
184
break;
185
case "H"://指定高,宽按比例
186
towidth = originalImage.Width * height / originalImage.Height;
187
break;
188
case "Cut"://指定高宽裁减(不变形)
189
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
190
{
191
oh = originalImage.Height;
192
ow = originalImage.Height * towidth / toheight;
193
y = 0;
194
x = (originalImage.Width - ow) / 2;
195
}
196
else
197
{
198
ow = originalImage.Width;
199
oh = originalImage.Width * height / towidth;
200
x = 0;
201
y = (originalImage.Height - oh) / 2;
202
}
203
break;
204
default:
205
break;
206
}
207
//新建一个bmp图片
208
Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
209
//新建一个画板
210
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
211
//设置高质量插值法
212
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
213
//设置高质量,低速度呈现平滑程度
214
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
215
//清空画布并以透明背景色填充
216
g.Clear(Color.Transparent);
217
//在指定位置并且按指定大小绘制原图片的指定部分
218
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
219
new Rectangle(x, y, ow, oh),
220
GraphicsUnit.Pixel);
221
try
222
{
223
//以jpg格式保存缩略图
224
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
225
}
226
catch (System.Exception e)
227
{
228
throw e;
229
}
230
finally
231
{
232
originalImage.Dispose();
233
bitmap.Dispose();
234
g.Dispose();
235
}
236
}
237
/**//// <summary>
238
/// 打水印,在某一点
239
///http://www.cnblogs.com/pooeo/
240
/// </summary>
241
/// <param name="image"></param>
242
/// <param name="waterImagePath"></param>
243
/// <param name="p"></param>
244
public static void Makewater(Image image, String waterImagePath, Point p)
245
{
246
ImageDeal.Makewater(image, waterImagePath, p, ImagePosition.TopLeft);
247
}
248
public static void Makewater(Image image, String waterImagePath, Point p, ImagePosition imagePosition)
249
{
250
using (Image warterImage = Image.FromFile(waterImagePath))
251
{
252
using (Graphics newGp = Graphics.FromImage(image))
253
{
254
newGp.CompositingQuality = CompositingQuality.HighQuality;
255
//设置高质量插值法
256
newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
257
//设置高质量,低速度呈现平滑程度
258
newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
259
switch (imagePosition)
260
{
261
case ImagePosition.BottomLeft:
262
p.Y = image.Height - warterImage.Height - p.Y;
263
break;
264
case ImagePosition.TopRigth:
265
p.X = image.Width - warterImage.Width - p.X;
266
break;
267
case ImagePosition.BottomRight:
268
p.Y = image.Height - warterImage.Height - p.Y;
269
p.X = image.Width - warterImage.Width - p.X;
270
break;
271
}
272
newGp.DrawImage(warterImage, new Rectangle(p, new Size(warterImage.Width, warterImage.Height)));
273
}
274
}
275
}
276
public static void Makewater(Image image, String waterStr, Font font, Brush brush, Point p)
277
{
278
ImageDeal.Makewater(image, waterStr, font, brush, p, ImagePosition.TopLeft);
279
}
280
public static void Makewater(Image image, String waterStr, Font font, Brush brush, Point p, ImagePosition imagePosition)
281
{
282
using (Graphics newGp = Graphics.FromImage(image))
283
{
284
Int32 stringWidth;
285
Int32 stringHeight;
286
stringHeight = (int)font.Size;
287
stringWidth = (int)(((float)StringDeal.GetBitLength(waterStr) / (float)2) * (font.Size + 1));
288
newGp.CompositingQuality = CompositingQuality.HighQuality;
289
//设置高质量插值法
290
newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
291
//设置高质量,低速度呈现平滑程度
292
newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
293
//文字抗锯齿
294
newGp.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
295
switch (imagePosition)
296
{
297
case ImagePosition.BottomLeft:
298
p.Y = image.Height - stringHeight - p.Y;
299
break;
300
case ImagePosition.TopRigth:
301
p.X = image.Width - stringWidth - p.X;
302
break;
303
case ImagePosition.BottomRight:
304
p.Y = image.Height - stringHeight - p.Y;
305
p.X = image.Width - stringWidth - p.X;
306
break;
307
}
308
newGp.DrawString(waterStr, font, brush, p);
309
}
310
}
311
/**//// <summary>
312
/// 高质量保存
313
///http://www.cnblogs.com/pooeo/
314
/// </summary>
315
/// <param name="image"></param>
316
/// <param name="path"></param>
317
public static void SaveQuality(Image image, String path)
318
{
319
ImageCodecInfo myImageCodecInfo;
320
Encoder myEncoder;
321
EncoderParameter myEncoderParameter;
322
EncoderParameters myEncoderParameters;
323
myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[0];
324
myEncoder = Encoder.Quality;
325
myEncoderParameters = new EncoderParameters(1);
326
myEncoderParameter = new EncoderParameter(myEncoder, 100L); // 0-100
327
myEncoderParameters.Param[0] = myEncoderParameter;
328
try
329
{
330
image.Save(path, myImageCodecInfo, myEncoderParameters);
331
}
332
finally
333
{
334
myEncoderParameter.Dispose();
335
myEncoderParameters.Dispose();
336
}
337
}
338
}
339
public enum StringPosition
340
{
341
TopLeft,
342
BottomLeft
343
}
344
public enum ImagePosition
345
{
346
TopLeft,
347
BottomLeft,
348
BottomRight,
349
TopRigth
350
}
351
}

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

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351
