分享动态生成文字图片解决方案
大家都知道我们如果想把网页上的文字做出比较炫的效果,便只能用POTOSHOP、FIREWORK等图像处理软件把文字做成图片来实现,因为这样才不会依赖浏览者的字体、浏览器类型等。可是在我们的WEB应用中又往往是动态的文字,我们便不能用图像处理软件来处理了,只能让WEB程序动态生成,幸运地是.Net Framework给我们提供了便利,下面我们就利用System.Drawing命名空间下的Bitmap类与Graphics类来编写一个生成文字图片的类,使用该类生成图片时能满足以下需求:
1、可以指定文字字体、大小和颜色(注:指定的文字在WEB服务器上需要有该字库);
2、可以加文字阴影;
3、可以指定文字的透明度;
4、可以指定背景图片或背景颜色;
5、可以指定生成的图片大小(宽度与高度);
6、可以指定文字的位置(左边距和上边距);
7、当用户设定的文字字号太大,能自动调整文字大小使之能适应生成图片的大小。
该类实现代码如下:
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}
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}
调用则相当简单,在此不予赘述,特把我使用的效果抓个图,以供大家参考
如果你有好的建议或疑惑都可以跟我联系MSN:AspSir#hotmail.com