asp.net 2.0 将文字转成图片

 《asp.net 2.0 将文字转成图片》
出处 :http:
//blog.csdn.net/LEOlws/archive/2006/12/30/1469370.aspx
作者:甜饼旺

---------------------------    代码开始   -----------------------------

CompNamePic.aspx(图片要显示在哪个页面中的图片框中)

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 
= "CompNamePic.aspx?Text=" + Server.UrlEncode(Text);
          
base.Render(writer);
      }

}




    
-----       -----      -----      -----         -----       -----      -----      -----


CompNamePic.aspx.cs(将生成的图片打印到网页):

using System;

public partial class Shop_CompNamePic : System.Web.UI.Page
{
      
...

      
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;

     
...

     
...

      
/**//// <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, 00);
          
// 新建一个内存流
          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;
      }

}



---------------------------    代码结束 -----------------------------

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值