服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#给图片添加水印完整实例

C#给图片添加水印完整实例

2021-11-05 11:02思齐_ C#

这篇文章主要介绍了C#给图片添加水印的方法,以完整实例形式分析了C#实现文字及图像水印、缩略图、图片剪切等相关技巧,需要的朋友可以参考下

本文实例讲述了C#给图片添加水印的方法。分享给大家供大家参考,具体如下:

?
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
/// <summary>
///ImgWater 的摘要说明
/// </summary>
public class ImgWater
{
 public ImgWater()
 {
  //
  //TODO: 在此处添加构造函数逻辑
  //
 }
 /// <summary>
 /// 图片水印
 /// </summary>
 /// <param name="ImgFile">原图文件地址</param>
 /// <param name="WaterImg">水印图片地址</param>
 /// <param name="sImgPath">水印图片保存地址</param>
 /// <param name="Alpha">水印透明度设置</param>
 /// <param name="iScale">水印图片在原图上的显示比例</param>
 /// <param name="intDistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param>
 public bool zzsImgWater(
   string ImgFile
  , string WaterImg
  , string sImgPath
  , float Alpha
  , float iScale
  , int intDistance
  )
  {
  try
  {
   //装载图片
   FileStream fs = new FileStream(ImgFile, FileMode.Open);
   BinaryReader br = new BinaryReader(fs);
   byte[] bytes = br.ReadBytes((int)fs.Length);
   br.Close();
   fs.Close();
   MemoryStream ms = new MemoryStream(bytes);
   System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
   //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
   int imgPhotoWidth = imgPhoto.Width;
   int imgPhotoHeight = imgPhoto.Height;
   System.Drawing.Image imgWatermark = new Bitmap(WaterImg);
   int imgWatermarkWidth = imgWatermark.Width;
   int imgWatermarkHeight = imgWatermark.Height;
   //计算水印图片尺寸
   decimal aScale = Convert.ToDecimal(iScale);
   decimal pScale = 0.05M;
   decimal MinScale = aScale - pScale;
   decimal MaxScale = aScale + pScale;
   int imgWatermarkWidthNew = imgWatermarkWidth;
   int imgWatermarkHeightNew = imgWatermarkHeight;
   if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight)
    if (imgWatermarkWidth > imgWatermarkHeight)
     if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale))
      {
     }
     else
      {
      imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
      imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight);
     }
    else
     if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale))
     {
     }
     else
      {
      imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
      imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth);
     }
   if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight)
    {
    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
   }
   if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
    {
    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
   }
   if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
    {
    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
   }
   if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight)
    if (imgWatermarkWidth > imgWatermarkHeight)
     if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale))
      {
     }
     else
      {
      imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
      imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
     }
    else
     if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale))
      {
     }
     else
      {
      imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
      imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
     }
   if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight)
    {
    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
   }
   if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
    {
    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
   }
   if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
    {
    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
   }
   //将原图画出来
   Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
   bmPhoto.SetResolution(72, 72);
   Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
   gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
   gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
   gbmPhoto.Clear(Color.White);
   gbmPhoto.DrawImage(
     imgPhoto
    , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
    , 0
    , 0
    , imgPhotoWidth
    , imgPhotoHeight
    , GraphicsUnit.Pixel
    );
   Bitmap bmWatermark = new Bitmap(bmPhoto);
   bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
   Graphics gWatermark = Graphics.FromImage(bmWatermark);
   //指定高质量显示水印图片质量
   gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
   gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    ImageAttributes imageAttributes = new ImageAttributes();
   //设置两种颜色,达到合成效果
   ColorMap colorMap = new ColorMap();
   colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
   colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
   ColorMap[] remapTable = { colorMap };
   imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap);
     //用矩阵设置水印图片透明度
   float[][] colorMatrixElements = {
    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, Alpha, 0.0f},
    new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
   };
   System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
   imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);
   //确定水印边距
   int xPos = imgPhotoWidth - imgWatermarkWidthNew;
   int yPos = imgPhotoHeight - imgWatermarkHeightNew;
   int xPosOfWm = 0;
   int yPosOfWm = 0;
   if (xPos > intDistance)
    xPosOfWm = xPos - intDistance;
   else
    xPosOfWm = xPos;
   if (yPos > intDistance)
    yPosOfWm = yPos - intDistance;
   else
    yPosOfWm = yPos;
   gWatermark.DrawImage(
     imgWatermark
    , new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew)
    , 0
    , 0
    , imgWatermarkWidth
    , imgWatermarkHeight
    , GraphicsUnit.Pixel
    , imageAttributes
    );
   imgPhoto = bmWatermark;
   //以jpg格式保存图片
   imgPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
   //销毁对象
   gbmPhoto.Dispose();
   gWatermark.Dispose();
   imgPhoto.Dispose();
   imgWatermark.Dispose();
   return true;
  }
  catch
  {
   return false ;
  }
 }
 /**//// <summary>
 /// 文字水印
 /// </summary>
 /// <param name="ImgFile">原图文件地址</param>
 /// <param name="TextFont">水印文字</param>
 /// <param name="sImgPath">文字水印图片保存地址</param>
 /// <param name="hScale">高度位置</param>
 /// <param name="widthFont">文字块在图片中所占宽度比例</param>
 /// <param name="Alpha">文字透明度 其数值的范围在0到255</param>
 public bool zzsTextWater(
   string ImgFile
  , string TextFont
  , string sImgPath
  , float hScale
  , float widthFont
  , int Alpha
  )
  {
  try
  {
   FileStream fs = new FileStream(ImgFile, FileMode.Open);
   BinaryReader br = new BinaryReader(fs);
   byte[] bytes = br.ReadBytes((int)fs.Length);
   br.Close();
   fs.Close();
   MemoryStream ms = new MemoryStream(bytes);
   System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
   //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
   int imgPhotoWidth = imgPhoto.Width;
   int imgPhotoHeight = imgPhoto.Height;
   Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
   bmPhoto.SetResolution(72, 72);
   Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
   gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
   gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
   gbmPhoto.DrawImage(
     imgPhoto
    , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
    , 0
    , 0
    , imgPhotoWidth
    , imgPhotoHeight
    , GraphicsUnit.Pixel
    );
   //建立字体大小的数组,循环找出适合图片的水印字体
   //int[] sizes = new int[] { 1000, 800, 700, 650, 600, 560, 540, 500, 450, 400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 72, 64, 48, 32, 28, 26, 24, 20, 28, 16, 14, 12, 10, 8, 6, 4, 2 };
   int[] sizes = new int[] { 28, 26, 24, 20, 16, 14, 12 };
   System.Drawing.Font crFont = null;
   System.Drawing.SizeF crSize = new SizeF();
   for (int i = 0; i < 7; i++)
   {
    crFont = new Font("微软雅黑", sizes[i], FontStyle.Bold);
    crSize = gbmPhoto.MeasureString(TextFont, crFont);
    if ((ushort)crSize.Width < (ushort)imgPhotoWidth * widthFont)
     break;
   }
   //设置水印字体的位置
   //int yPixlesFromBottom = (int)(imgPhotoHeight * hScale);
   //float yPosFromBottom = ((imgPhotoHeight - yPixlesFromBottom) - (crSize.Height / 2));
   //float xCenterOfImg = (imgPhotoWidth * 1 / 2);
   float yPosFromBottom = imgPhotoHeight *0.85f;
   float xCenterOfImg = imgPhotoWidth * 0.8f;
   //if (xCenterOfImg<crSize.Height)
   // xCenterOfImg = (imgPhotoWidth * (1 - (crSize.Height)/ imgPhotoWidth));
   System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
   StrFormat.Alignment = System.Drawing.StringAlignment.Center;
   //
   System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(Color.FromArgb(Alpha, 0, 0, 0));
   gbmPhoto.DrawString(
     TextFont
    , crFont
    , semiTransBrush2
    , new System.Drawing.PointF(xCenterOfImg + 1, yPosFromBottom + 1)
    , StrFormat
    );
   System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(Color.FromArgb(Alpha, 255, 255, 255));
   //gbmPhoto.FillRectangle(semiTransBrush2, new RectangleF(new PointF(xCenterOfImg - crSize.Width / 2, yPosFromBottom - 4), crSize));
   gbmPhoto.DrawString(
     TextFont
    , crFont
    , semiTransBrush
    , new System.Drawing.PointF(xCenterOfImg, yPosFromBottom)
    , StrFormat
    );
   bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
   gbmPhoto.Dispose();
   imgPhoto.Dispose();
   bmPhoto.Dispose();
   return true;
  }
  catch
  {
   return false;
  }
 }
 /**//// <summary>
 /// 文字和Logo图片水印
 /// </summary>
 /// <param name="ImgFile">原图文件地址</param>
 /// <param name="WaterImg">水印图片地址</param>
 /// <param name="TextFont">水印文字信息</param>
 /// <param name="sImgPath">生存水印图片后的保存地址</param>
 /// <param name="ImgAlpha">水印图片的透明度</param>
 /// <param name="imgiScale">水印图片在原图上的显示比例</param>
 /// <param name="intimgDistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param>
 /// <param name="texthScale">水印文字高度位置,从图片底部开始计算,0-1</param>
 /// <param name="textwidthFont">文字块在图片中所占宽度比例 0-1</param>
 /// <param name="textAlpha">文字透明度 其数值的范围在0到255</param>
 public void zzsImgTextWater(
   string ImgFile
  , string WaterImg
  , string TextFont
  , string sImgPath
  , float ImgAlpha
  , float imgiScale
  , int intimgDistance
  , float texthScale
  , float textwidthFont
  , int textAlpha
  )
  {
  try
  {
   FileStream fs = new FileStream(ImgFile, FileMode.Open);
   BinaryReader br = new BinaryReader(fs);
   byte[] bytes = br.ReadBytes((int)fs.Length);
   br.Close();
   fs.Close();
   MemoryStream ms = new MemoryStream(bytes);
   System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
   //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
   int imgPhotoWidth = imgPhoto.Width;
   int imgPhotoHeight = imgPhoto.Height;
   Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
   bmPhoto.SetResolution(72, 72);
   Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
   gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
   gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
   gbmPhoto.DrawImage(
     imgPhoto
    , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
    , 0
    , 0
    , imgPhotoWidth
    , imgPhotoHeight
    , GraphicsUnit.Pixel
    );
   //建立字体大小的数组,循环找出适合图片的水印字体
   int[] sizes = new int[] { 1000, 800, 700, 650, 600, 560, 540, 500, 450, 400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 72, 64, 48, 32, 28, 26, 24, 20, 28, 16, 14, 12, 10, 8, 6, 4, 2 };
   System.Drawing.Font crFont = null;
   System.Drawing.SizeF crSize = new SizeF();
   for (int i = 0; i < 43; i++)
   {
    crFont = new Font("arial", sizes[i], FontStyle.Bold);
    crSize = gbmPhoto.MeasureString(TextFont, crFont);
    if ((ushort)crSize.Width < (ushort)imgPhotoWidth * textwidthFont)
     break;
   }
   //设置水印字体的位置
   int yPixlesFromBottom = (int)(imgPhotoHeight * texthScale);
   float yPosFromBottom = ((imgPhotoHeight - yPixlesFromBottom) - (crSize.Height / 2));
   float xCenterOfImg = (imgPhotoWidth * 1 / 2);
   System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
   StrFormat.Alignment = System.Drawing.StringAlignment.Center;
   //
   System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(Color.FromArgb(textAlpha, 0, 0, 0));
   gbmPhoto.DrawString(
     TextFont
    , crFont
    , semiTransBrush2
    , new System.Drawing.PointF(xCenterOfImg + 1, yPosFromBottom + 1)
    , StrFormat
    );
   System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(Color.FromArgb(textAlpha, 255, 255, 255));
   gbmPhoto.DrawString(
     TextFont
    , crFont
    , semiTransBrush
    , new System.Drawing.PointF(xCenterOfImg, yPosFromBottom)
    , StrFormat
    );
   System.Drawing.Image imgWatermark = new Bitmap(WaterImg);
   int imgWatermarkWidth = imgWatermark.Width;
   int imgWatermarkHeight = imgWatermark.Height;
   //计算水印图片尺寸
   decimal aScale = Convert.ToDecimal(imgiScale);
   decimal pScale = 0.05M;
   decimal MinScale = aScale - pScale;
   decimal MaxScale = aScale + pScale;
   int imgWatermarkWidthNew = imgWatermarkWidth;
   int imgWatermarkHeightNew = imgWatermarkHeight;
   if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight)
    if (imgWatermarkWidth > imgWatermarkHeight)
     if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale))
     {
     }
     else
     {
      imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
      imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight);
     }
    else
     if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale))
     {
     }
     else
     {
      imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
      imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth);
     }
   if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight)
   {
    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
   }
   if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
   {
    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
   }
   if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
   {
    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
   }
   if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight)
    if (imgWatermarkWidth > imgWatermarkHeight)
     if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale))
     {
     }
     else
     {
      imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
      imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
     }
    else
     if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale))
     {
     }
     else
     {
      imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
      imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
     }
   if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight)
   {
    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
   }
   if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
   {
    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
   }
   if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
   {
    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
   }
   //将原图画出来
   Bitmap bmWatermark = new Bitmap(bmPhoto);
   bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
   Graphics gWatermark = Graphics.FromImage(bmWatermark);
   //指定高质量显示水印图片质量
   gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
   gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
   System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes();
   //设置两种颜色,达到合成效果
   System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap();
   colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
   colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
   System.Drawing.Imaging.ColorMap[] remapTable = { colorMap };
   imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap);
   //用矩阵设置水印图片透明度
   float[][] colorMatrixElements = {
    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, ImgAlpha, 0.0f},
    new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
   };
   System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
   imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);
   //确定水印边距
   int xPos = imgPhotoWidth - imgWatermarkWidthNew;
   int yPos = imgPhotoHeight - imgWatermarkHeightNew;
   int xPosOfWm = 0;
   int yPosOfWm = 0;
   if (xPos > intimgDistance)
    xPosOfWm = xPos - intimgDistance;
   else
    xPosOfWm = xPos;
   if (yPos > intimgDistance)
    yPosOfWm = yPos - intimgDistance;
   else
    yPosOfWm = yPos;
   gWatermark.DrawImage(
     imgWatermark
    , new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew)
    , 0
    , 0
    , imgWatermarkWidth
    , imgWatermarkHeight
    , GraphicsUnit.Pixel
    , imageAttributes
    );
   imgPhoto = bmWatermark;
   //以jpg格式保存图片
   imgPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
   //销毁对象
   gbmPhoto.Dispose();
   gWatermark.Dispose();
   bmPhoto.Dispose();
   imgPhoto.Dispose();
   imgWatermark.Dispose();
  }
  catch
  {
  }
 }
 /**//// <summary>
 /// 缩略图
 /// </summary>
 /// <param name="ImgFile">原图文件地址</param>
 /// <param name="sImgPath">缩略图保存地址</param>
 /// <param name="ResizeWidth">缩略图宽度</param>
 /// <param name="ResizeHeight">缩略图高度</param>
 /// <param name="BgColor">缩略图背景颜色,注意,背景颜色只能指定KnownColor中的值,如blue,red,green等</param>
 public bool zzsResizeImg( string ImgFile , string sImgPath , int ResizeWidth , int ResizeHeight , string BgColor )
  {
  try
  {
   FileStream fs = new FileStream(ImgFile, FileMode.Open);
   BinaryReader br = new BinaryReader(fs);
   byte[] bytes = br.ReadBytes((int)fs.Length);
   br.Close();
   fs.Close();
   MemoryStream ms = new MemoryStream(bytes);
   System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
   //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
   int imgPhotoWidth = imgPhoto.Width;
   int imgPhotoHeight = imgPhoto.Height;
   int startX = 0;
   int StartY = 0;
   int NewWidth = 0;
   int NewHeight = 0;
   if (imgPhotoWidth >= ResizeWidth && imgPhotoHeight >= ResizeHeight)
   {
    NewWidth = ResizeWidth;
    NewHeight = Convert.ToInt32(imgPhotoHeight * Math.Round(Convert.ToDecimal(ResizeWidth) / Convert.ToDecimal(imgPhotoWidth), 10));
    startX = 0;
    StartY = (ResizeHeight - NewHeight) / 2;
   }
   if (ResizeWidth > imgPhotoWidth && ResizeHeight < imgPhotoHeight)
   {
    NewHeight = ResizeHeight;
    NewWidth = Convert.ToInt32(imgPhotoWidth * Math.Round(Convert.ToDecimal(ResizeHeight) / Convert.ToDecimal(imgPhotoHeight), 10));
    startX = (ResizeWidth - NewWidth) / 2;
    StartY = 0;
   }
   if (ResizeWidth < imgPhotoWidth && ResizeHeight > imgPhotoHeight)
   {
    NewWidth = ResizeWidth;
    NewHeight = Convert.ToInt32(imgPhotoHeight * Math.Round(Convert.ToDecimal(ResizeWidth) / Convert.ToDecimal(imgPhotoWidth), 10));
    startX = 0;
    StartY = (ResizeHeight - NewHeight) / 2;
   }
   if (imgPhotoWidth < ResizeWidth && imgPhotoHeight < ResizeHeight)
   {
    NewWidth = imgPhotoWidth;
    NewHeight = imgPhotoHeight;
    startX = (ResizeWidth - imgPhotoWidth) / 2;
    StartY = (ResizeHeight - imgPhotoHeight) / 2;
   }
   //计算缩放图片尺寸
   Bitmap bmPhoto = new Bitmap(ResizeWidth, ResizeHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
   bmPhoto.SetResolution(72, 72);
   Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
   gbmPhoto.Clear(Color.FromName(BgColor));
   gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
   gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
   gbmPhoto.DrawImage(
     imgPhoto
    , new Rectangle(startX, StartY, NewWidth, NewHeight)
    , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
    , GraphicsUnit.Pixel
    );
   bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
   imgPhoto.Dispose();
   gbmPhoto.Dispose();
   bmPhoto.Dispose();
   ms.Close();
   return true;
  }
  catch
  {
   return false;
  }
 }
 /**//// <summary>
 /// 图片剪切
 /// </summary>
 /// <param name="ImgFile">原图文件地址</param>
 /// <param name="sImgPath">缩略图保存地址</param>
 /// <param name="PointX">剪切起始点 X坐标</param>
 /// <param name="PointY">剪切起始点 Y坐标</param>
 /// <param name="CutWidth">剪切宽度</param>
 /// <param name="CutHeight">剪切高度</param>
 public bool zzsCutImg(string ImgFile, string sImgPath, int PointX, int PointY, int CutWidth, int CutHeight)
 {
  FileStream fs = new FileStream(ImgFile, FileMode.Open);
  BinaryReader br = new BinaryReader(fs);
  try
  {
   byte[] bytes = br.ReadBytes((int)fs.Length);
   br.Close();
   fs.Close();
   MemoryStream ms = new MemoryStream(bytes);
   System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
   //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
   //此处只能用filestream,用 System.Drawing.Image则会报多过进程访问文件的错误,会锁定文件
   Bitmap bmPhoto = new Bitmap(CutWidth, CutHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
   bmPhoto.SetResolution(72, 72);
   Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
   gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
   gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
   gbmPhoto.DrawImage(
     imgPhoto
    , new Rectangle(0, 0, CutWidth, CutHeight)
    , new Rectangle(PointX, PointY, CutHeight, CutHeight)
    , GraphicsUnit.Pixel
    );
   bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
   imgPhoto.Dispose();
   gbmPhoto.Dispose();
   bmPhoto.Dispose();
   ms.Close();
   return true;
  }
  catch
  {
   return false;
  }
  finally
  {
  }
 }
}

希望本文所述对大家C#程序设计有所帮助。

延伸 · 阅读

精彩推荐
  • C#深入理解C#的数组

    深入理解C#的数组

    本篇文章主要介绍了C#的数组,数组是一种数据结构,详细的介绍了数组的声明和访问等,有兴趣的可以了解一下。...

    佳园9492021-12-10
  • C#C#微信公众号与订阅号接口开发示例代码

    C#微信公众号与订阅号接口开发示例代码

    这篇文章主要介绍了C#微信公众号与订阅号接口开发示例代码,结合实例形式简单分析了C#针对微信接口的调用与处理技巧,需要的朋友可以参考下...

    smartsmile20127762021-11-25
  • C#C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    这篇文章主要介绍了C#设计模式之Strategy策略模式解决007大破密码危机问题,简单描述了策略模式的定义并结合加密解密算法实例分析了C#策略模式的具体使用...

    GhostRider10972022-01-21
  • C#三十分钟快速掌握C# 6.0知识点

    三十分钟快速掌握C# 6.0知识点

    这篇文章主要介绍了C# 6.0的相关知识点,文中介绍的非常详细,通过这篇文字可以让大家在三十分钟内快速的掌握C# 6.0,需要的朋友可以参考借鉴,下面来...

    雨夜潇湘8272021-12-28
  • C#利用C#实现网络爬虫

    利用C#实现网络爬虫

    这篇文章主要介绍了利用C#实现网络爬虫,完整的介绍了C#实现网络爬虫详细过程,感兴趣的小伙伴们可以参考一下...

    C#教程网11852021-11-16
  • C#VS2012 程序打包部署图文详解

    VS2012 程序打包部署图文详解

    VS2012虽然没有集成打包工具,但它为我们提供了下载的端口,需要我们手动安装一个插件InstallShield。网上有很多第三方的打包工具,但为什么偏要使用微软...

    张信秀7712021-12-15
  • C#如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    这篇文章主要给大家介绍了关于如何使用C#将Tensorflow训练的.pb文件用在生产环境的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴...

    bbird201811792022-03-05
  • C#SQLite在C#中的安装与操作技巧

    SQLite在C#中的安装与操作技巧

    SQLite,是一款轻型的数据库,用于本地的数据储存。其优点有很多,下面通过本文给大家介绍SQLite在C#中的安装与操作技巧,感兴趣的的朋友参考下吧...

    蓝曈魅11162022-01-20