C#.NET 图片水印添加代码

时间:2021-09-23 13:23:27

本文实例为大家分享了C#.NET添加 图片水印的方法,供大家参考,具体内容如下

?
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
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
 
namespace Pub.Class
{
 /// <summary>
 /// 添加水印类 只支持添加图片水印
 /// </summary>
 public class Watermark
 {
  #region 私有成员
  private string modifyImagePath=null;
 private string drawedImagePath=null;
 private int rightSpace;
 private int bottoamSpace;
 private int lucencyPercent=70;
 private string outPath=null;
  #endregion
 
  #region 构造器
  /// <summary>
  /// 构造函数
  /// </summary>
  public Watermark() { }
  #endregion
 
  #region 属性
  /// <summary>
 /// 获取或设置要修改的图像路径
 /// </summary>
 public string ModifyImagePath
 {
 get{return this.modifyImagePath;}
 set{this.modifyImagePath=value;}
 }
 /// <summary>
 /// 获取或设置在画的图片路径(水印图片)
 /// </summary>
 public string DrawedImagePath
 {
 get{return this.drawedImagePath;}
 set{this.drawedImagePath=value;}
 }
 /// <summary>
 /// 获取或设置水印在修改图片中的右边距
 /// </summary>
 public int RightSpace
 {
 get{return this.rightSpace;}
 set{this.rightSpace=value;}
 }
 /// <summary>
  /// 获取或设置水印在修改图片中距底部的高度
 /// </summary>
 public int BottoamSpace
 {
 get{return this.bottoamSpace;}
 set{this.bottoamSpace=value;}
 }
 /// <summary>
 /// 获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比
 /// </summary>
 public int LucencyPercent
 {
 get{return this.lucencyPercent;}
 set { if(value>=0&&value<=100) this.lucencyPercent=value; }
 }
 /// <summary>
 /// 获取或设置要输出图像的路径
 /// </summary>
 public string OutPath
 {
 get{return this.outPath;}
 set{this.outPath=value;}
  }
  #endregion
 
  #region 开始绘制水印 DrawImage
  /// <summary>
 /// 开始绘制水印
 /// </summary>
  /// <example>
  /// <code>
  ///  Watermark wm = new Watermark();
  ///  wm.DrawedImagePath= Server.MapPath("") + "/upfile/" + "backlogo.gif";
  ///  wm.ModifyImagePath=path;
  ///  wm.RightSpace=184;
  ///  wm.BottoamSpace=81;
  ///  wm.LucencyPercent=50;
  ///  wm.OutPath=Server.MapPath("") + "/upfile/" + fileName + "_new" + extension;
  ///  wm.DrawImage();
  /// 
  ///  //保存加水印过后的图片,删除原始图片
  ///  mFileName=fileName + "_new" + extension;
  ///  if(File.Exists(path)) { File.Delete(path); }
  /// </code>
  /// </example>
 public void DrawImage()
 {
 Image modifyImage=null;
 Image drawedImage=null;
 Graphics g=null;
 try {
 modifyImage=Image.FromFile(this.ModifyImagePath);//建立图形对象
 drawedImage=Image.FromFile(this.DrawedImagePath);
 g=Graphics.FromImage(modifyImage);
 
 int x=modifyImage.Width-this.rightSpace;//获取要绘制图形坐标
 int y=modifyImage.Height-this.BottoamSpace;
 
 float[][] matrixItems ={//设置颜色矩阵
   new float[] {1, 0, 0, 0, 0},
   new float[] {0, 1, 0, 0, 0},
   new float[] {0, 0, 1, 0, 0},
   new float[] {0, 0, 0, (float)this.LucencyPercent/100f, 0},
   new float[] {0, 0, 0, 0, 1}};
 
 ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
 ImageAttributes imgAttr=new ImageAttributes();
 imgAttr.SetColorMatrix(colorMatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap);
 
 g.DrawImage(//绘制阴影图像
  drawedImage,
  new Rectangle(x,y,drawedImage.Width,drawedImage.Height),
  0,0,drawedImage.Width,drawedImage.Height,
  GraphicsUnit.Pixel,imgAttr);
 
 string[] allowImageType={".jpg",".gif",".png",".bmp",".tiff",".wmf",".ico"};//保存文件
 FileInfo file=new FileInfo(this.ModifyImagePath);
 ImageFormat imageType=ImageFormat.Gif;
 switch(file.Extension.ToLower()) {
  case ".jpg": imageType=ImageFormat.Jpeg; break;
  case ".gif": imageType=ImageFormat.Gif; break;
  case ".png": imageType=ImageFormat.Png; break;
  case ".bmp": imageType=ImageFormat.Bmp; break;
  case ".tif": imageType=ImageFormat.Tiff; break;
  case ".wmf": imageType=ImageFormat.Wmf; break;
  case ".ico": imageType=ImageFormat.Icon; break;
  default: break;
 }
 MemoryStream ms=new MemoryStream();
 modifyImage.Save(ms,imageType);
 byte[] imgData=ms.ToArray();
 modifyImage.Dispose();
 drawedImage.Dispose();
 g.Dispose();
 FileStream fs=null;
 if(this.OutPath==null || this.OutPath=="") {
  File.Delete(this.ModifyImagePath);
  fs=new FileStream(this.ModifyImagePath,FileMode.Create,FileAccess.Write);
 } else {
  fs=new FileStream(this.OutPath,FileMode.Create,FileAccess.Write);
 }
 if(fs!=null) {
  fs.Write(imgData,0,imgData.Length);
  fs.Close();
 }
 } finally {
 try {
  drawedImage.Dispose();
  modifyImage.Dispose();
  g.Dispose();
 } catch{}
 }
 }
  #endregion
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/livexy/archive/2010/07/05/1771714.html

相关文章