我是靠谱客的博主 美满月饼,这篇文章主要介绍IO帮助类是什么?IO帮助类的实例介绍(附代码),现在分享给大家,希望可以做个参考。

本篇文章给大家带来的内容是介绍IO帮助类是什么?IO帮助类的实例介绍(附代码)。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。

复制代码
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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
using System; using System.IO; using System.Web; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Xml.Serialization; namespace ZMM.Core { /// <summary> /// IO帮助类 /// </summary> public class IOHelper { //是否已经加载了JPEG编码解码器 private static bool _isloadjpegcodec = false; //当前系统安装的JPEG编码解码器 private static ImageCodecInfo _jpegcodec = null; /// <summary> /// 获得文件物理路径 /// </summary> /// <returns></returns> public static string GetMapPath(string path) { if (HttpContext.Current != null) { return HttpContext.Current.Server.MapPath(path); } else { return System.Web.Hosting.HostingEnvironment.MapPath(path); } } #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="obj">序列对象</param> /// <param name="filePath">XML文件路径</param> /// <returns>是否成功</returns> public static bool SerializeToXml(object obj, string filePath) { bool result = false; FileStream fs = null; try { fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); XmlSerializer serializer = new XmlSerializer(obj.GetType()); serializer.Serialize(fs, obj); result = true; } catch (Exception ex) { throw ex; } finally { if (fs != null) fs.Close(); } return result; } /// <summary> /// XML反序列化 /// </summary> /// <param name="type">目标类型(Type类型)</param> /// <param name="filePath">XML文件路径</param> /// <returns>序列对象</returns> public static object DeserializeFromXML(Type type, string filePath) { FileStream fs = null; try { fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); XmlSerializer serializer = new XmlSerializer(type); return serializer.Deserialize(fs); } catch (Exception ex) { throw ex; } finally { if (fs != null) fs.Close(); } } #endregion #region 水印,缩略图 /// <summary> /// 获得当前系统安装的JPEG编码解码器 /// </summary> /// <returns></returns> public static ImageCodecInfo GetJPEGCodec() { if (_isloadjpegcodec == true) return _jpegcodec; ImageCodecInfo[] codecsList = ImageCodecInfo.GetImageEncoders(); foreach (ImageCodecInfo codec in codecsList) { if (codec.MimeType.IndexOf("jpeg") > -1) { _jpegcodec = codec; break; } } _isloadjpegcodec = true; return _jpegcodec; } /// <summary> /// 生成缩略图 /// </summary> /// <param name="imagePath">图片路径</param> /// <param name="thumbPath">缩略图路径</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void GenerateThumb(string imagePath, string thumbPath, int width, int height, string mode) { Image image = Image.FromFile(imagePath); string extension = imagePath.Substring(imagePath.LastIndexOf(".")).ToLower(); ImageFormat imageFormat = null; switch (extension) { case ".jpg": case ".jpeg": imageFormat = ImageFormat.Jpeg; break; case ".bmp": imageFormat = ImageFormat.Bmp; break; case ".png": imageFormat = ImageFormat.Png; break; case ".gif": imageFormat = ImageFormat.Gif; break; default: imageFormat = ImageFormat.Jpeg; break; } int toWidth = width > 0 ? width : image.Width; int toHeight = height > 0 ? height : image.Height; int x = 0; int y = 0; int ow = image.Width; int oh = image.Height; switch (mode) { case "HW"://指定高宽缩放(可能变形) break; case "W"://指定宽,高按比例 toHeight = image.Height * width / image.Width; break; case "H"://指定高,宽按比例 toWidth = image.Width * height / image.Height; break; case "Cut"://指定高宽裁减(不变形) if ((double)image.Width / (double)image.Height > (double)toWidth / (double)toHeight) { oh = image.Height; ow = image.Height * toWidth / toHeight; y = 0; x = (image.Width - ow) / 2; } else { ow = image.Width; oh = image.Width * height / toWidth; x = 0; y = (image.Height - oh) / 2; } break; default: break; } //新建一个bmp Image bitmap = new Bitmap(toWidth, toHeight); //新建一个画板 Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(image, new Rectangle(0, 0, toWidth, toHeight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); try { bitmap.Save(thumbPath, imageFormat); } catch (Exception ex) { throw ex; } finally { if (g != null) g.Dispose(); if (bitmap != null) bitmap.Dispose(); if (image != null) image.Dispose(); } } /// <summary> /// 生成图片水印 /// </summary> /// <param name="originalPath">源图路径</param> /// <param name="watermarkPath">水印图片路径</param> /// <param name="targetPath">保存路径</param> /// <param name="position">位置</param> /// <param name="opacity">透明度</param> /// <param name="quality">质量</param> public static void GenerateImageWatermark(string originalPath, string watermarkPath, string targetPath, int position, int opacity, int quality) { Image originalImage = null; Image watermarkImage = null; //图片属性 ImageAttributes attributes = null; //画板 Graphics g = null; try { originalImage = Image.FromFile(originalPath); watermarkImage = new Bitmap(watermarkPath); if (watermarkImage.Height >= originalImage.Height || watermarkImage.Width >= originalImage.Width) { originalImage.Save(targetPath); return; } if (quality < 0 || quality > 100) quality = 80; //水印透明度 float iii; if (opacity > 0 && opacity <= 10) iii = (float)(opacity / 10.0F); else iii = 0.5F; //水印位置 int x = 0; int y = 0; switch (position) { case 1: x = (int)(originalImage.Width * (float).01); y = (int)(originalImage.Height * (float).01); break; case 2: x = (int)((originalImage.Width * (float).50) - (watermarkImage.Width / 2)); y = (int)(originalImage.Height * (float).01); break; case 3: x = (int)((originalImage.Width * (float).99) - (watermarkImage.Width)); y = (int)(originalImage.Height * (float).01); break; case 4: x = (int)(originalImage.Width * (float).01); y = (int)((originalImage.Height * (float).50) - (watermarkImage.Height / 2)); break; case 5: x = (int)((originalImage.Width * (float).50) - (watermarkImage.Width / 2)); y = (int)((originalImage.Height * (float).50) - (watermarkImage.Height / 2)); break; case 6: x = (int)((originalImage.Width * (float).99) - (watermarkImage.Width)); y = (int)((originalImage.Height * (float).50) - (watermarkImage.Height / 2)); break; case 7: x = (int)(originalImage.Width * (float).01); y = (int)((originalImage.Height * (float).99) - watermarkImage.Height); break; case 8: x = (int)((originalImage.Width * (float).50) - (watermarkImage.Width / 2)); y = (int)((originalImage.Height * (float).99) - watermarkImage.Height); break; case 9: x = (int)((originalImage.Width * (float).99) - (watermarkImage.Width)); y = (int)((originalImage.Height * (float).99) - watermarkImage.Height); break; } //颜色映射表 ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] newColorMap = { colorMap }; //颜色变换矩阵,iii是设置透明度的范围0到1中的单精度类型 float[][] newColorMatrix ={ new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, iii, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; //定义一个 5 x 5 矩阵 ColorMatrix matrix = new ColorMatrix(newColorMatrix); //图片属性 attributes = new ImageAttributes(); attributes.SetRemapTable(newColorMap, ColorAdjustType.Bitmap); attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); //画板 g = Graphics.FromImage(originalImage); //绘制水印 g.DrawImage(watermarkImage, new Rectangle(x, y, watermarkImage.Width, watermarkImage.Height), 0, 0, watermarkImage.Width, watermarkImage.Height, GraphicsUnit.Pixel, attributes); //保存图片 EncoderParameters encoderParams = new EncoderParameters(); encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, new long[] { quality }); if (GetJPEGCodec() != null) originalImage.Save(targetPath, _jpegcodec, encoderParams); else originalImage.Save(targetPath); } catch (Exception ex) { throw ex; } finally { if (g != null) g.Dispose(); if (attributes != null) attributes.Dispose(); if (watermarkImage != null) watermarkImage.Dispose(); if (originalImage != null) originalImage.Dispose(); } } /// <summary> /// 生成文字水印 /// </summary> /// <param name="originalPath">源图路径</param> /// <param name="targetPath">保存路径</param> /// <param name="text">水印文字</param> /// <param name="textSize">文字大小</param> /// <param name="textFont">文字字体</param> /// <param name="position">位置</param> /// <param name="quality">质量</param> public static void GenerateTextWatermark(string originalPath, string targetPath, string text, int textSize, string textFont, int position, int quality) { Image originalImage = null; //画板 Graphics g = null; try { originalImage = Image.FromFile(originalPath); //画板 g = Graphics.FromImage(originalImage); if (quality < 0 || quality > 100) quality = 80; Font font = new Font(textFont, textSize, FontStyle.Regular, GraphicsUnit.Pixel); SizeF sizePair = g.MeasureString(text, font); float x = 0; float y = 0; switch (position) { case 1: x = (float)originalImage.Width * (float).01; y = (float)originalImage.Height * (float).01; break; case 2: x = ((float)originalImage.Width * (float).50) - (sizePair.Width / 2); y = (float)originalImage.Height * (float).01; break; case 3: x = ((float)originalImage.Width * (float).99) - sizePair.Width; y = (float)originalImage.Height * (float).01; break; case 4: x = (float)originalImage.Width * (float).01; y = ((float)originalImage.Height * (float).50) - (sizePair.Height / 2); break; case 5: x = ((float)originalImage.Width * (float).50) - (sizePair.Width / 2); y = ((float)originalImage.Height * (float).50) - (sizePair.Height / 2); break; case 6: x = ((float)originalImage.Width * (float).99) - sizePair.Width; y = ((float)originalImage.Height * (float).50) - (sizePair.Height / 2); break; case 7: x = (float)originalImage.Width * (float).01; y = ((float)originalImage.Height * (float).99) - sizePair.Height; break; case 8: x = ((float)originalImage.Width * (float).50) - (sizePair.Width / 2); y = ((float)originalImage.Height * (float).99) - sizePair.Height; break; case 9: x = ((float)originalImage.Width * (float).99) - sizePair.Width; y = ((float)originalImage.Height * (float).99) - sizePair.Height; break; } g.DrawString(text, font, new SolidBrush(Color.White), x + 1, y + 1); g.DrawString(text, font, new SolidBrush(Color.Black), x, y); //保存图片 EncoderParameters encoderParams = new EncoderParameters(); encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, new long[] { quality }); if (GetJPEGCodec() != null) originalImage.Save(targetPath, _jpegcodec, encoderParams); else originalImage.Save(targetPath); } catch (Exception ex) { throw ex; } finally { if (g != null) g.Dispose(); if (originalImage != null) originalImage.Dispose(); } } #endregion } }
登录后复制

以上就是IO帮助类是什么?IO帮助类的实例介绍(附代码)的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是美满月饼最近收集整理的关于IO帮助类是什么?IO帮助类的实例介绍(附代码)的全部内容,更多相关IO帮助类是什么?IO帮助类内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(89)

评论列表共有 0 条评论

立即
投稿
返回
顶部