該類實現代碼如下:
1using System.Drawing;
2using System.Drawing.Drawing2D;
3using System.Drawing.Imaging;
4
5namespace Ycweb.Controls.Utility
6{
7 /**//// <summary>
8 /// WaterMark
9 /// </summary>
10 public class Watermark
11 {
12 private int _width;
13 private int _height;
14 private string _fontFamily;
15 private int _fontSize;
16 private bool _adaptable;
17 private FontStyle _fontStyle;
18 private bool _shadow;
19 private string _backgroundImage;
20 private Color _bgColor;
21 private int _left;
22 private string _resultImage;
23 private string _text;
24 private int _top;
25 private int _alpha;
26 private int _red;
27 private int _green;
28 private int _blue;
29 private long _quality;
30
31
32
33 public Watermark()
34 {
35 //
36 // TODO: Add constructor logic here
37 //
38 _width=460;
39 _height=30;
40 _fontFamily = "華文行楷";
41 _fontSize = 20;
42 _fontStyle=FontStyle.Regular;
43 _adaptable=true;
44 _shadow=false;
45 _left = 0;
46 _top = 0;
47 _alpha = 255;
48 _red = 0;
49 _green = 0;
50 _blue = 0;
51 _backgroundImage="";
52 _quality=100;
53 _bgColor=Color.FromArgb(255,229,229,229);
54
55 }
56
57 /**//// <summary>
58 /// 字體
59 /// </summary>
60 public string FontFamily
61 {
62 set { this._fontFamily = value; }
63 }
64
65 /**//// <summary>
66 /// 文字大小
67 /// </summary>
68 public int FontSize
69 {
70 set { this._fontSize = value; }
71 }
72
73 /**//// <summary>
74 /// 文字風格
75 /// </summary>
76 public FontStyle FontStyle
77 {
78 get{return _fontStyle;}
79 set{_fontStyle = value;}
80 }
81
82 /**//// <summary>
83 /// 透明度0-255,255表示不透明
84 /// </summary>
85 public int Alpha
86 {
87 get { return _alpha; }
88 set { _alpha = value; }
89 }
90
91 /**//// <summary>
92 /// 水印文字是否使用陰影
93 /// </summary>
94 public bool Shadow
95 {
96 get { return _shadow; }
97 set { _shadow = value; }
98 }
99
100 public int Red
101 {
102 get { return _red; }
103 set { _red = value; }
104 }
105
106 public int Green
107 {
108 get { return _green; }
109 set { _green = value; }
110 }
111
112 public int Blue
113 {
114 get { return _blue; }
115 set { _blue = value; }
116 }
117
118 /**//// <summary>
119 /// 底圖
120 /// </summary>
121 public string BackgroundImage
122 {
123 set { this._backgroundImage = value; }
124 }
125
126 /**//// <summary>
127 /// 水印文字的左邊距
128 /// </summary>
129 public int Left
130 {
131 set { this._left = value; }
132 }
133
134
135 /**//// <summary>
136 /// 水印文字的頂邊距
137 /// </summary>
138 public int Top
139 {
140 set { this._top = value; }
141 }
142
143 /**//// <summary>
144 /// 生成後的圖片
145 /// </summary>
146 public string ResultImage
147 {
148 set { this._resultImage = value; }
149 }
150
151 /**//// <summary>
152 /// 水印文本
153 /// </summary>
154 public string Text
155 {
156 set { this._text = value; }
157 }
158
159
160 /**//// <summary>
161 /// 生成圖片的寬度
162 /// </summary>
163 public int Width
164 {
165 get { return _width; }
166 set { _width = value; }
167 }
168
169 /**//// <summary>
170 /// 生成圖片的高度
171 /// </summary>
172 public int Height
173 {
174 get { return _height; }
175 set { _height = value; }
176 }
177
178 /**//// <summary>
179 /// 若文字太大,是否根據背景圖來調整文字大小,默認為適應
180 /// </summary>
181 public bool Adaptable
182 {
183 get { return _adaptable; }
184 set { _adaptable = value; }
185 }
186
187 public Color BgColor
188 {
189 get { return _bgColor; }
190 set { _bgColor = value; }
191 }
192
193 /**//// <summary>
194 /// 輸出圖片質量,質量范圍0-100,類型為long
195 /// </summary>
196 public long Quality
197 {
198 get { return _quality; }
199 set { _quality = value; }
200 }
201
202 /**//// <summary>
203 /// 立即生成水印效果圖
204 /// </summary>
205 /// <returns>生成成功返回true,否則返回false</returns>
206 public bool Create()
207 {
208 try
209 {
210 Bitmap bitmap;
211 Graphics g;
212
213 //使用純背景色
214 if(this._backgroundImage.Trim()=="")
215 {
216 bitmap = new Bitmap(this._width, this._height, PixelFormat.Format64bppArgb);
217 g = Graphics.FromImage(bitmap);
218 g.Clear(this._bgColor);
219 }
220 else
221 {
222 bitmap = new Bitmap(Image.FromFile(this._backgroundImage));
223 g = Graphics.FromImage(bitmap);
224 }
225 g.SmoothingMode = SmoothingMode.HighQuality;
226 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
227 g.CompositingQuality=CompositingQuality.HighQuality;
228
229 Font f = new Font(_fontFamily, _fontSize,_fontStyle);
230 SizeF size = g.MeasureString(_text, f);
231
232 // 調整文字大小直到能適應圖片尺寸
233 while(_adaptable==true && size.Width > bitmap.Width)
234 {
235 _fontSize--;
236 f = new Font(_fontFamily, _fontSize, _fontStyle);
237 size = g.MeasureString(_text, f);
238 }
239
240 Brush b = new SolidBrush(Color.FromArgb(_alpha, _red, _green, _blue));
241 StringFormat StrFormat = new StringFormat();
242 StrFormat.Alignment = StringAlignment.Near;
243
244 if(this._shadow)
245 {
246 Brush b2=new SolidBrush(Color.FromArgb(90, 0, 0, 0));
247 g.DrawString(_text, f, b2,_left+2, _top+1);
248 }
249 g.DrawString(_text, f, b, new PointF(_left, _top), StrFormat);
250
251 bitmap.Save(this._resultImage, ImageFormat.Jpeg);
252 bitmap.Dispose();
253 g.Dispose();
254 return true;
255 }
256 catch
257 {
258 return false;
259 }
260 }
261 }
262
263
264}
調用則相當簡單,在此不予贅述,特把我使用的效果抓個圖,以供大家參考