分享动态生成文字图片解决方案

       大家都知道我们如果想把网页上的文字做出比较炫的效果,便只能用POTOSHOP、FIREWORK等图像处理软件把文字做成图片来实现,因为这样才不会依赖浏览者的字体、浏览器类型等。可是在我们的WEB应用中又往往是动态的文字,我们便不能用图像处理软件来处理了,只能让WEB程序动态生成,幸运地是.Net Framework给我们提供了便利,下面我们就利用System.Drawing命名空间下的Bitmap类与Graphics类来编写一个生成文字图片的类,使用该类生成图片时能满足以下需求:
1、可以指定文字字体、大小和颜色(注:指定的文字在WEB服务器上需要有该字库);
2、可以加文字阴影;
3、可以指定文字的透明度;
4、可以指定背景图片或背景颜色;
5、可以指定生成的图片大小(宽度与高度);
6、可以指定文字的位置(左边距和上边距);
7、当用户设定的文字字号太大,能自动调整文字大小使之能适应生成图片的大小。

该类实现代码如下:

  1 None.gif using  System.Drawing;
  2 None.gif using  System.Drawing.Drawing2D;
  3 None.gif using  System.Drawing.Imaging;
  4 None.gif
  5 None.gif namespace  Ycweb.Controls.Utility
  6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  8InBlock.gif    /// WaterMark
  9ExpandedSubBlockEnd.gif    /// </summary>

 10InBlock.gif    public class Watermark
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12InBlock.gif        private int _width;
 13InBlock.gif        private int _height;
 14InBlock.gif        private string _fontFamily;
 15InBlock.gif        private int _fontSize;
 16InBlock.gif        private bool _adaptable;
 17InBlock.gif        private FontStyle _fontStyle;
 18InBlock.gif        private bool _shadow;
 19InBlock.gif        private string _backgroundImage;
 20InBlock.gif        private Color _bgColor;
 21InBlock.gif        private int _left;
 22InBlock.gif        private string _resultImage;
 23InBlock.gif        private string _text;
 24InBlock.gif        private int _top;
 25InBlock.gif        private int _alpha;
 26InBlock.gif        private int _red;
 27InBlock.gif        private int _green;
 28InBlock.gif        private int _blue;
 29InBlock.gif        private long _quality;
 30InBlock.gif
 31InBlock.gif
 32InBlock.gif
 33InBlock.gif        public Watermark()
 34ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 35InBlock.gif            //
 36InBlock.gif            // TODO: Add constructor logic here
 37InBlock.gif            //
 38InBlock.gif            _width=460;
 39InBlock.gif            _height=30;
 40InBlock.gif            _fontFamily = "华文行楷";
 41InBlock.gif            _fontSize = 20;
 42InBlock.gif            _fontStyle=FontStyle.Regular;
 43InBlock.gif            _adaptable=true;
 44InBlock.gif            _shadow=false;
 45InBlock.gif            _left = 0;
 46InBlock.gif            _top = 0;
 47InBlock.gif            _alpha = 255;
 48InBlock.gif            _red = 0;
 49InBlock.gif            _green = 0;
 50InBlock.gif            _blue = 0;
 51InBlock.gif            _backgroundImage="";
 52InBlock.gif            _quality=100;
 53InBlock.gif            _bgColor=Color.FromArgb(255,229,229,229);
 54InBlock.gif            
 55ExpandedSubBlockEnd.gif        }

 56InBlock.gif
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 58InBlock.gif        /// 字体
 59ExpandedSubBlockEnd.gif        /// </summary>

 60InBlock.gif        public string FontFamily
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 62ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis._fontFamily = value; }
 63ExpandedSubBlockEnd.gif        }

 64InBlock.gif
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 66InBlock.gif        /// 文字大小
 67ExpandedSubBlockEnd.gif        /// </summary>

 68InBlock.gif        public int FontSize
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 70ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis._fontSize = value; }
 71ExpandedSubBlockEnd.gif        }

 72InBlock.gif
 73ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 74InBlock.gif        /// 文字风格
 75ExpandedSubBlockEnd.gif        /// </summary>

 76InBlock.gif        public FontStyle FontStyle
 77ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 78ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return _fontStyle;}
 79ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gif{_fontStyle = value;}
 80ExpandedSubBlockEnd.gif        }

 81InBlock.gif
 82ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 83InBlock.gif        /// 透明度0-255,255表示不透明
 84ExpandedSubBlockEnd.gif        /// </summary>

 85InBlock.gif        public int Alpha
 86ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 87ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _alpha; }
 88ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _alpha = value; }
 89ExpandedSubBlockEnd.gif        }

 90InBlock.gif
 91ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 92InBlock.gif        /// 水印文字是否使用阴影
 93ExpandedSubBlockEnd.gif        /// </summary>

 94InBlock.gif        public bool Shadow
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _shadow; }
 97ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _shadow = value; }
 98ExpandedSubBlockEnd.gif        }

 99InBlock.gif
100InBlock.gif        public int Red
101ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
102ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _red; }
103ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _red = value; }
104ExpandedSubBlockEnd.gif        }

105InBlock.gif
106InBlock.gif        public int Green
107ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
108ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _green; }
109ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _green = value; }
110ExpandedSubBlockEnd.gif        }

111InBlock.gif
112InBlock.gif        public int Blue
113ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
114ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _blue; }
115ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _blue = value; }
116ExpandedSubBlockEnd.gif        }

117InBlock.gif
118ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
119InBlock.gif        /// 底图
120ExpandedSubBlockEnd.gif        /// </summary>

121InBlock.gif        public string BackgroundImage
122ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
123ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis._backgroundImage = value; }
124ExpandedSubBlockEnd.gif        }

125InBlock.gif
126ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
127InBlock.gif        /// 水印文字的左边距
128ExpandedSubBlockEnd.gif        /// </summary>

129InBlock.gif        public int Left
130ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
131ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis._left = value; }
132ExpandedSubBlockEnd.gif        }

133InBlock.gif
134InBlock.gif        
135ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
136InBlock.gif        /// 水印文字的顶边距
137ExpandedSubBlockEnd.gif        /// </summary>

138InBlock.gif        public int Top
139ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
140ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis._top = value; }
141ExpandedSubBlockEnd.gif        }

142InBlock.gif
143ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
144InBlock.gif        /// 生成后的图片
145ExpandedSubBlockEnd.gif        /// </summary>

146InBlock.gif        public string ResultImage
147ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
148ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis._resultImage = value; }
149ExpandedSubBlockEnd.gif        }

150InBlock.gif
151ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
152InBlock.gif        /// 水印文本
153ExpandedSubBlockEnd.gif        /// </summary>

154InBlock.gif        public string Text
155ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
156ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis._text = value; }
157ExpandedSubBlockEnd.gif        }

158InBlock.gif
159InBlock.gif        
160ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
161InBlock.gif        /// 生成图片的宽度
162ExpandedSubBlockEnd.gif        /// </summary>

163InBlock.gif        public int Width
164ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
165ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _width; }
166ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _width = value; }
167ExpandedSubBlockEnd.gif        }

168InBlock.gif
169ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
170InBlock.gif        /// 生成图片的高度
171ExpandedSubBlockEnd.gif        /// </summary>

172InBlock.gif        public int Height
173ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
174ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _height; }
175ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _height = value; }
176ExpandedSubBlockEnd.gif        }

177InBlock.gif
178ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
179InBlock.gif        /// 若文字太大,是否根据背景图来调整文字大小,默认为适应
180ExpandedSubBlockEnd.gif        /// </summary>

181InBlock.gif        public bool Adaptable
182ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
183ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _adaptable; }
184ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _adaptable = value; }
185ExpandedSubBlockEnd.gif        }

186InBlock.gif
187InBlock.gif        public Color BgColor
188ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
189ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _bgColor; }
190ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _bgColor = value; }
191ExpandedSubBlockEnd.gif        }

192InBlock.gif
193ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
194InBlock.gif        /// 输出图片质量,质量范围0-100,类型为long
195ExpandedSubBlockEnd.gif        /// </summary>

196InBlock.gif        public long Quality
197ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
198ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _quality; }
199ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _quality = value; }
200ExpandedSubBlockEnd.gif        }

201InBlock.gif
202ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
203InBlock.gif        /// 立即生成水印效果图
204InBlock.gif        /// </summary>
205ExpandedSubBlockEnd.gif        /// <returns>生成成功返回true,否则返回false</returns>

206InBlock.gif        public bool Create()
207ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
208InBlock.gif            try
209ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
210InBlock.gif                Bitmap bitmap;
211InBlock.gif                Graphics g;
212InBlock.gif
213InBlock.gif                //使用纯背景色
214InBlock.gif                if(this._backgroundImage.Trim()=="")
215ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
216InBlock.gif                    bitmap = new Bitmap(this._width, this._height, PixelFormat.Format64bppArgb);
217InBlock.gif                    g = Graphics.FromImage(bitmap);
218InBlock.gif                    g.Clear(this._bgColor);
219ExpandedSubBlockEnd.gif                }

220InBlock.gif                else
221ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
222InBlock.gif                    bitmap = new Bitmap(Image.FromFile(this._backgroundImage));
223InBlock.gif                    g = Graphics.FromImage(bitmap);
224ExpandedSubBlockEnd.gif                }

225InBlock.gif                g.SmoothingMode = SmoothingMode.HighQuality;
226InBlock.gif                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
227InBlock.gif                g.CompositingQuality=CompositingQuality.HighQuality;
228InBlock.gif
229InBlock.gif                Font f = new Font(_fontFamily, _fontSize,_fontStyle);
230InBlock.gif                SizeF size = g.MeasureString(_text, f);
231InBlock.gif                
232InBlock.gif                // 调整文字大小直到能适应图片尺寸
233InBlock.gif                while(_adaptable==true && size.Width > bitmap.Width) 
234ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
235InBlock.gif                    _fontSize--;
236InBlock.gif                    f = new Font(_fontFamily, _fontSize, _fontStyle);
237InBlock.gif                    size = g.MeasureString(_text, f);
238ExpandedSubBlockEnd.gif                }

239InBlock.gif                
240InBlock.gif                Brush b = new SolidBrush(Color.FromArgb(_alpha, _red, _green, _blue));
241InBlock.gif                StringFormat StrFormat = new StringFormat();
242InBlock.gif                StrFormat.Alignment = StringAlignment.Near;
243InBlock.gif
244InBlock.gif                if(this._shadow)
245ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
246InBlock.gif                    Brush b2=new SolidBrush(Color.FromArgb(90000));
247InBlock.gif                    g.DrawString(_text, f, b2,_left+2, _top+1);
248ExpandedSubBlockEnd.gif                }

249InBlock.gif                g.DrawString(_text, f, b, new PointF(_left, _top), StrFormat);
250InBlock.gif                
251InBlock.gif                bitmap.Save(this._resultImage, ImageFormat.Jpeg);
252InBlock.gif                bitmap.Dispose();
253InBlock.gif                g.Dispose();
254InBlock.gif                return true;
255ExpandedSubBlockEnd.gif            }

256InBlock.gif            catch
257ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
258InBlock.gif                return false;
259ExpandedSubBlockEnd.gif            }

260ExpandedSubBlockEnd.gif        }

261ExpandedSubBlockEnd.gif    }

262InBlock.gif
263InBlock.gif
264ExpandedBlockEnd.gif}

调用则相当简单,在此不予赘述,特把我使用的效果抓个图,以供大家参考

screenshot.png
如果你有好的建议或疑惑都可以跟我联系MSN:AspSir#hotmail.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值