asp.net 2.0 将文字转换成图片
来源:互联网 发布:广电网络股票吧 编辑:程序博客网 时间:2024/05/16 05:52
图片要显示在哪个页面中的图片框中...
CompNamePic.Text = "需要显示的文字";
CompNamePic.ascx.cs
用于显示自定义文字图片的控件
using System;
public partial class Shop_UC_CompNamePic : System.Web.UI.UserControl
...{
private string _Text = null;
+ 属性#region + 属性
public string Text
...{
get
...{
if (_Text == null || _Text.Trim().Length <= 0)
...{
return "";
}
return _Text;
}
set ...{ _Text = value; }
}
#endregion
protected void Page_Load(object sender, EventArgs e)
...{
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
...{
imgText.ImageUrl = "~/Shop/CompNamePic.aspx?Text=" + Server.UrlEncode(Text);
base.Render(writer);
}
}
public partial class Shop_UC_CompNamePic : System.Web.UI.UserControl
...{
private string _Text = null;
+ 属性#region + 属性
public string Text
...{
get
...{
if (_Text == null || _Text.Trim().Length <= 0)
...{
return "";
}
return _Text;
}
set ...{ _Text = value; }
}
#endregion
protected void Page_Load(object sender, EventArgs e)
...{
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
...{
imgText.ImageUrl = "~/Shop/CompNamePic.aspx?Text=" + Server.UrlEncode(Text);
base.Render(writer);
}
}
CompNamePic.aspx.cs
将生成的图片打印到网页
using System;
public partial class Shop_CompNamePic : System.Web.UI.Page
...{
+ 属性#region + 属性
private string CompName
...{
get
...{
if (Request.QueryString["Text"] == null || Request.QueryString["Text"].Trim().Length <= 0)
...{
return "";
}
return Server.UrlDecode(Request.QueryString["Text"]);
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
...{
CreateImgText cit = new CreateImgText(CompName);
Response.BinaryWrite(cit.CreateTextByte());
}
}
public partial class Shop_CompNamePic : System.Web.UI.Page
...{
+ 属性#region + 属性
private string CompName
...{
get
...{
if (Request.QueryString["Text"] == null || Request.QueryString["Text"].Trim().Length <= 0)
...{
return "";
}
return Server.UrlDecode(Request.QueryString["Text"]);
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
...{
CreateImgText cit = new CreateImgText(CompName);
Response.BinaryWrite(cit.CreateTextByte());
}
}
CreateImgText.cs
用于将文字转换成图片的类
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
/**//// <summary>
/// 创建图片文字
/// </summary>
public class CreateImgText
...{
private string _Text = null;
private Color _TextColor = Color.Empty;
private FontStyle _TextStyle = FontStyle.Bold;
private int _Width;
private int _Height;
+ 属性#region + 属性
/**//// <summary>
/// 要显示的文本
/// </summary>
public string Text
...{
get
...{
if (_Text == null || _Text.Trim().Length <= 0)
...{
return "图片加载中...";
}
return _Text;
}
set ...{ _Text = value; }
}
/**//// <summary>
/// 文本颜色
/// </summary>
public Color TextColor
...{
get
...{
if (_TextColor == Color.Empty)
...{
return Color.Black;
}
return _TextColor;
}
set ...{ _TextColor = value; }
}
/**//// <summary>
/// 文本样式 加粗 倾斜 普通等
/// </summary>
public FontStyle TextStyle
...{
get ...{ return _TextStyle; }
set ...{ _TextStyle = value; }
}
/**//// <summary>
/// 图片的宽
/// </summary>
public int Width
...{
get ...{ return _Width; }
//set { _Width = value; }
}
/**//// <summary>
/// 图片的高
/// </summary>
public int Height
...{
get ...{ return _Height; }
//set { _Height = value; }
}
#endregion
+ 构造 + 3#region + 构造 + 3
public CreateImgText()
...{
}
public CreateImgText(string strText)
...{
_Text = strText;
}
public CreateImgText(string strText,Color TextColor)
...{
_Text = strText;
_TextColor = TextColor;
}
public CreateImgText(string strText, Color TextColor, FontStyle TextStyle)
...{
_Text = strText;
_TextColor = TextColor;
_TextStyle = TextStyle;
}
#endregion
/**//// <summary>
/// 创建输出的文字流
/// </summary>
/// <returns></returns>
public byte[] CreateTextByte()
...{
Font font = new Font("黑体", 20, TextStyle);
Brush brush = new SolidBrush(TextColor);
// 计算文字的宽和高
Size sizeText = TextRenderer.MeasureText(Text, font);
_Width = sizeText.Width;
_Height = sizeText.Height;
// 创建一个位图
Bitmap bmp = new Bitmap(sizeText.Width, sizeText.Height);
// 设置画布
Graphics grph = Graphics.FromImage(bmp);
// 指定消除锯齿 文字
grph.TextRenderingHint = TextRenderingHint.AntiAlias;
// 清除画布
grph.Clear(Color.White);
// 在画布上画图案 内容,字体,画刷,坐标
grph.DrawString(Text, font, brush, 0, 0);
// 新建一个内存流
MemoryStream stream = new MemoryStream();
// 将图片保存在内存流中
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] byteBuf = new byte[stream.Length];
byteBuf = stream.ToArray();
//资源回收
bmp.Dispose();
grph.Dispose();
stream.Close();
return byteBuf;
}
}
using System.IO;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
/**//// <summary>
/// 创建图片文字
/// </summary>
public class CreateImgText
...{
private string _Text = null;
private Color _TextColor = Color.Empty;
private FontStyle _TextStyle = FontStyle.Bold;
private int _Width;
private int _Height;
+ 属性#region + 属性
/**//// <summary>
/// 要显示的文本
/// </summary>
public string Text
...{
get
...{
if (_Text == null || _Text.Trim().Length <= 0)
...{
return "图片加载中...";
}
return _Text;
}
set ...{ _Text = value; }
}
/**//// <summary>
/// 文本颜色
/// </summary>
public Color TextColor
...{
get
...{
if (_TextColor == Color.Empty)
...{
return Color.Black;
}
return _TextColor;
}
set ...{ _TextColor = value; }
}
/**//// <summary>
/// 文本样式 加粗 倾斜 普通等
/// </summary>
public FontStyle TextStyle
...{
get ...{ return _TextStyle; }
set ...{ _TextStyle = value; }
}
/**//// <summary>
/// 图片的宽
/// </summary>
public int Width
...{
get ...{ return _Width; }
//set { _Width = value; }
}
/**//// <summary>
/// 图片的高
/// </summary>
public int Height
...{
get ...{ return _Height; }
//set { _Height = value; }
}
#endregion
+ 构造 + 3#region + 构造 + 3
public CreateImgText()
...{
}
public CreateImgText(string strText)
...{
_Text = strText;
}
public CreateImgText(string strText,Color TextColor)
...{
_Text = strText;
_TextColor = TextColor;
}
public CreateImgText(string strText, Color TextColor, FontStyle TextStyle)
...{
_Text = strText;
_TextColor = TextColor;
_TextStyle = TextStyle;
}
#endregion
/**//// <summary>
/// 创建输出的文字流
/// </summary>
/// <returns></returns>
public byte[] CreateTextByte()
...{
Font font = new Font("黑体", 20, TextStyle);
Brush brush = new SolidBrush(TextColor);
// 计算文字的宽和高
Size sizeText = TextRenderer.MeasureText(Text, font);
_Width = sizeText.Width;
_Height = sizeText.Height;
// 创建一个位图
Bitmap bmp = new Bitmap(sizeText.Width, sizeText.Height);
// 设置画布
Graphics grph = Graphics.FromImage(bmp);
// 指定消除锯齿 文字
grph.TextRenderingHint = TextRenderingHint.AntiAlias;
// 清除画布
grph.Clear(Color.White);
// 在画布上画图案 内容,字体,画刷,坐标
grph.DrawString(Text, font, brush, 0, 0);
// 新建一个内存流
MemoryStream stream = new MemoryStream();
// 将图片保存在内存流中
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] byteBuf = new byte[stream.Length];
byteBuf = stream.ToArray();
//资源回收
bmp.Dispose();
grph.Dispose();
stream.Close();
return byteBuf;
}
}
已测试.
- asp.net 2.0 将文字转换成图片
- asp.net 2.0 将文字转成图片
- asp.net将文字绘制成图片
- Asp.net将图片转换成二进制
- 将文字转换成图片
- asp.net 2.0 将文字转成图片(文字生成图片)
- python将文字转换成图片
- 怎样将图片转换成word文字
- ASP.net生成文字图片
- ASP.net生成文字图片
- ASP.net生成文字图片
- ASP.net生成文字图片
- ASP.net生成文字图片
- ASP.net生成文字图片
- ASP.net生成文字图片
- php使用gd库将文字转换成图片
- ADO.NET ASP.NET 将 SQLDatareader 转换成 List
- ASP.NET:图片添加文字信息
- HTMLENCODE和URLENCODE的区别
- 设置tomcat5.5以上的jvm最大内存值
- 龙芯软件开发(13)--配置南桥进入调试新天地
- 大型网站新闻系统如何生成静态网页
- 12月26日——培训第32天
- asp.net 2.0 将文字转换成图片
- 计算机程序设计课程教学之我见
- 程序员真实写真:35岁前成功的12条黄金法则
- Struts控制器组件
- 12月28日——培训第33天
- 不同页面转到同一页面后,返回到不同页面的实现
- 一份详尽的IPC$入侵资料
- 奥运是搜狐的奥运,还是全中国的奥运?
- 软件需求说明书