动态生成文字图片解决方案
大家都知道我们如果想把网页上的文字做出比较炫的效果,便只能用POTOSHOP、FIREWORK等图像处理软件把文字做成图片来实现,因为这样才不会依赖浏览者的字体、浏览器类型等。可是在我们的WEB应用中又往往是动态的文字,我们便不能用图像处理软件来处理了,只能让WEB程序动态生成,幸运地是.Net Framework给我们提供了便利,下面我们就利用System.Drawing命名空间下的Bitmap类与Graphics类来编写一个生成文字图片的类,使用该类生成图片时能满足以下需求:
1、可以指定文字字体、大小和颜色(注:指定的文字在WEB服务器上需要有该字库);
2、可以加文字阴影;
3、可以指定文字的透明度;
4、可以指定背景图片或背景颜色;
5、可以指定生成的图片大小(宽度与高度);
6、可以指定文字的位置(左边距和上边距);
7、当用户设定的文字字号太大,能自动调整文字大小使之能适应生成图片的大小。
该类实现代码如下:
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Ycweb.Controls.Utility
{
/**//// <summary>
/// WaterMark
/// </summary>
public class Watermark
{
private int _width; //背景宽度
private int _height; //背景高度
private string _fontFamily; //字体属性
private int _fontSize; //字体大小
private bool _adaptable; //若文字太大,是否根据背景图来调整文字大小,默认为适应
private FontStyle _fontStyle; //文字风格
private bool _shadow; //水印文字是否使用阴影
private string _backgroundImage; //背景图片
private Color _bgColor; //背景颜色
private int _left; //水印文字的左边距
private string _resultImage; // 生成后的图片
private string _text; //文字
private int _top; // 水印文字的顶边距
private int _alpha; //透明度0-255,255表示不透明
private int _red;
private int _green;
private int _blue;
private long _quality; //输出图片质量,质量范围0-100,类型为long
public Watermark()
{
//
// TODO: Add constructor logic here
//
_width=460;
_height=30;
_fontFamily = "华文行楷";
_fontSize = 20;
_fontStyle=FontStyle.Regular;
_adaptable=false ;
_shadow=false;
_left = 0;
_top = 0;
_alpha = 255;
_red = 0;
_green = 0;
_blue = 0;
_backgroundImage="";
_quality=100;
_bgColor=Color.FromArgb(255,229,229,229);
}
/**//// <summary>
/// 字体
/// </summary>
public string FontFamily
{
set { this._fontFamily = value; }
}
/**//// <summary>
/// 文字大小
/// </summary>
public int FontSize
{
set { this._fontSize = value; }
}
/**//// <summary>
/// 文字风格
/// </summary>
public FontStyle FontStyle
{
get{return _fontStyle;}
set{_fontStyle = value;}
}
/**//// <summary>
/// 透明度0-255,255表示不透明
/// </summary>
public int Alpha
{
get { return _alpha; }
set { _alpha = value; }
}
/**//// <summary>
/// 水印文字是否使用阴影
/// </summary>
public bool Shadow
{
get { return _shadow; }
set { _shadow = value; }
}
public int Red
{
get { return _red; }
set { _red = value; }
}
public int Green
{
get { return _green; }
set { _green = value; }
}
public int Blue
{
get { return _blue; }
set { _blue = value; }
}
/**//// <summary>
/// 底图
/// </summary>
public string BackgroundImage
{
set { this._backgroundImage = value; }
}
/**//// <summary>
/// 水印文字的左边距
/// </summary>
public int Left
{
set { this._left = value; }
}
/**//// <summary>
/// 水印文字的顶边距
/// </summary>
public int Top
{
set { this._top = value; }
}
/**//// <summary>
/// 生成后的图片
/// </summary>
public string ResultImage
{
set { this._resultImage = value; }
}
/**//// <summary>
/// 水印文本
/// </summary>
public string Text
{
set { this._text = value; }
}
/**//// <summary>
/// 生成图片的宽度
/// </summary>
public int Width
{
get { return _width; }
set { _width = value; }
}
/**//// <summary>
/// 生成图片的高度
/// </summary>
public int Height
{
get { return _height; }
set { _height = value; }
}
/**//// <summary>
/// 若文字太大,是否根据背景图来调整文字大小,默认为适应
/// </summary>
public bool Adaptable
{
get { return _adaptable; }
set { _adaptable = value; }
}
public Color BgColor
{
get { return _bgColor; }
set { _bgColor = value; }
}
/**//// <summary>
/// 输出图片质量,质量范围0-100,类型为long
/// </summary>
public long Quality
{
get { return _quality; }
set { _quality = value; }
}
/**//// <summary>
/// 立即生成水印效果图
/// </summary>
/// <returns>生成成功返回true,否则返回false</returns>
public bool Create()
{
try
{
Bitmap bitmap;
Graphics g;
//使用纯背景色
if(this._backgroundImage.Trim()=="")
{
bitmap = new Bitmap(this._width, this._height, PixelFormat.Format64bppArgb);
g = Graphics.FromImage(bitmap);
g.Clear(this._bgColor);
}
else
{
bitmap = new Bitmap(Image.FromFile(this._backgroundImage));
g = Graphics.FromImage(bitmap);
}
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality=CompositingQuality.HighQuality;
Font f = new Font(_fontFamily, _fontSize,_fontStyle);
SizeF size = g.MeasureString(_text, f);
// 调整文字大小直到能适应图片尺寸
while(_adaptable==true && size.Width > bitmap.Width)
{
_fontSize--;
f = new Font(_fontFamily, _fontSize, _fontStyle);
size = g.MeasureString(_text, f);
}
Brush b = new SolidBrush(Color.FromArgb(_alpha, _red, _green, _blue));
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Near;
if(this._shadow)
{
Brush b2=new SolidBrush(Color.FromArgb(90, 0, 0, 0));
g.DrawString(_text, f, b2,_left+2, _top+1);
}
g.DrawString(_text, f, b, new PointF(_left, _top), StrFormat);
bitmap.Save(this._resultImage, ImageFormat.Jpeg);
bitmap.Dispose();
g.Dispose();
return true;
}
catch
{
return false;
}
}
}
}
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Ycweb.Controls.Utility
{
/**//// <summary>
/// WaterMark
/// </summary>
public class Watermark
{
private int _width; //背景宽度
private int _height; //背景高度
private string _fontFamily; //字体属性
private int _fontSize; //字体大小
private bool _adaptable; //若文字太大,是否根据背景图来调整文字大小,默认为适应
private FontStyle _fontStyle; //文字风格
private bool _shadow; //水印文字是否使用阴影
private string _backgroundImage; //背景图片
private Color _bgColor; //背景颜色
private int _left; //水印文字的左边距
private string _resultImage; // 生成后的图片
private string _text; //文字
private int _top; // 水印文字的顶边距
private int _alpha; //透明度0-255,255表示不透明
private int _red;
private int _green;
private int _blue;
private long _quality; //输出图片质量,质量范围0-100,类型为long
public Watermark()
{
//
// TODO: Add constructor logic here
//
_width=460;
_height=30;
_fontFamily = "华文行楷";
_fontSize = 20;
_fontStyle=FontStyle.Regular;
_adaptable=false ;
_shadow=false;
_left = 0;
_top = 0;
_alpha = 255;
_red = 0;
_green = 0;
_blue = 0;
_backgroundImage="";
_quality=100;
_bgColor=Color.FromArgb(255,229,229,229);
}
/**//// <summary>
/// 字体
/// </summary>
public string FontFamily
{
set { this._fontFamily = value; }
}
/**//// <summary>
/// 文字大小
/// </summary>
public int FontSize
{
set { this._fontSize = value; }
}
/**//// <summary>
/// 文字风格
/// </summary>
public FontStyle FontStyle
{
get{return _fontStyle;}
set{_fontStyle = value;}
}
/**//// <summary>
/// 透明度0-255,255表示不透明
/// </summary>
public int Alpha
{
get { return _alpha; }
set { _alpha = value; }
}
/**//// <summary>
/// 水印文字是否使用阴影
/// </summary>
public bool Shadow
{
get { return _shadow; }
set { _shadow = value; }
}
public int Red
{
get { return _red; }
set { _red = value; }
}
public int Green
{
get { return _green; }
set { _green = value; }
}
public int Blue
{
get { return _blue; }
set { _blue = value; }
}
/**//// <summary>
/// 底图
/// </summary>
public string BackgroundImage
{
set { this._backgroundImage = value; }
}
/**//// <summary>
/// 水印文字的左边距
/// </summary>
public int Left
{
set { this._left = value; }
}
/**//// <summary>
/// 水印文字的顶边距
/// </summary>
public int Top
{
set { this._top = value; }
}
/**//// <summary>
/// 生成后的图片
/// </summary>
public string ResultImage
{
set { this._resultImage = value; }
}
/**//// <summary>
/// 水印文本
/// </summary>
public string Text
{
set { this._text = value; }
}
/**//// <summary>
/// 生成图片的宽度
/// </summary>
public int Width
{
get { return _width; }
set { _width = value; }
}
/**//// <summary>
/// 生成图片的高度
/// </summary>
public int Height
{
get { return _height; }
set { _height = value; }
}
/**//// <summary>
/// 若文字太大,是否根据背景图来调整文字大小,默认为适应
/// </summary>
public bool Adaptable
{
get { return _adaptable; }
set { _adaptable = value; }
}
public Color BgColor
{
get { return _bgColor; }
set { _bgColor = value; }
}
/**//// <summary>
/// 输出图片质量,质量范围0-100,类型为long
/// </summary>
public long Quality
{
get { return _quality; }
set { _quality = value; }
}
/**//// <summary>
/// 立即生成水印效果图
/// </summary>
/// <returns>生成成功返回true,否则返回false</returns>
public bool Create()
{
try
{
Bitmap bitmap;
Graphics g;
//使用纯背景色
if(this._backgroundImage.Trim()=="")
{
bitmap = new Bitmap(this._width, this._height, PixelFormat.Format64bppArgb);
g = Graphics.FromImage(bitmap);
g.Clear(this._bgColor);
}
else
{
bitmap = new Bitmap(Image.FromFile(this._backgroundImage));
g = Graphics.FromImage(bitmap);
}
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality=CompositingQuality.HighQuality;
Font f = new Font(_fontFamily, _fontSize,_fontStyle);
SizeF size = g.MeasureString(_text, f);
// 调整文字大小直到能适应图片尺寸
while(_adaptable==true && size.Width > bitmap.Width)
{
_fontSize--;
f = new Font(_fontFamily, _fontSize, _fontStyle);
size = g.MeasureString(_text, f);
}
Brush b = new SolidBrush(Color.FromArgb(_alpha, _red, _green, _blue));
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Near;
if(this._shadow)
{
Brush b2=new SolidBrush(Color.FromArgb(90, 0, 0, 0));
g.DrawString(_text, f, b2,_left+2, _top+1);
}
g.DrawString(_text, f, b, new PointF(_left, _top), StrFormat);
bitmap.Save(this._resultImage, ImageFormat.Jpeg);
bitmap.Dispose();
g.Dispose();
return true;
}
catch
{
return false;
}
}
}
}
调用方法如下
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ycweb.Controls.Utility;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Watermark _Watermark = new Watermark();
_Watermark.Top = 0;
_Watermark.Left = 0;
_Watermark.Height = 200;
_Watermark.Width = 600;
_Watermark.Quality = 100;
_Watermark.Red = 20;
_Watermark.ResultImage = @"c:\test.jpg"; //注意要有写的权限
_Watermark.Shadow = true;
_Watermark.Text = "你好啊????";
_Watermark.Adaptable = true;
_Watermark.Alpha = 255;
_Watermark.BgColor = System.Drawing.Color.Red;
_Watermark.Blue = 20;
_Watermark.FontFamily = "隶书";
_Watermark.FontSize = 14;
bool result=_Watermark.Create();
if(result)
{
Response.Write("OK");
}
else
{
Response.Write("Error");
}
Response.End();
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ycweb.Controls.Utility;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Watermark _Watermark = new Watermark();
_Watermark.Top = 0;
_Watermark.Left = 0;
_Watermark.Height = 200;
_Watermark.Width = 600;
_Watermark.Quality = 100;
_Watermark.Red = 20;
_Watermark.ResultImage = @"c:\test.jpg"; //注意要有写的权限
_Watermark.Shadow = true;
_Watermark.Text = "你好啊????";
_Watermark.Adaptable = true;
_Watermark.Alpha = 255;
_Watermark.BgColor = System.Drawing.Color.Red;
_Watermark.Blue = 20;
_Watermark.FontFamily = "隶书";
_Watermark.FontSize = 14;
bool result=_Watermark.Create();
if(result)
{
Response.Write("OK");
}
else
{
Response.Write("Error");
}
Response.End();
}
}
呵呵 转载的 出处http://www.cnblogs.com/aspsir/archive/2006/04/30/389513.html