该类实现代码如下:
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}
调用则相当简单,在此不予赘述,特把我使用的效果抓个图,以供大家参考
新闻热点
疑难解答