asp.net 2.0 将文字转换成图片

本文介绍了一个ASP.NET应用程序,该程序通过用户输入的文字生成自定义图片。使用CreateImgText类将文字转换为图片,并通过CompNamePic控件显示在网页上。

 图片要显示在哪个页面中的图片框中...

CompNamePic.Text = "需要显示的文字";


CompNamePic.ascx.cs
用于显示自定义文字图片的控件

using System;

public partial class Shop_UC_CompNamePic : System.Web.UI.UserControl
{
    
private string _Text = null;

    
+ 属性

    
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
{
    
+ 属性

    
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;

    
+ 属性

    
+ 构造 + 3

    
/// <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;
    }

}

已测试.

// AddTextDlg.cpp : implementation file // #include "stdafx.h" #include "Test.h" #include "AddTextDlg.h" #include ".\addtextdlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAddTextDlg dialog CAddTextDlg::CAddTextDlg(CWnd* pParent /*=NULL*/) : CDialog(CAddTextDlg::IDD, pParent) { //{{AFX_DATA_INIT(CAddTextDlg) m_strText = _T(""); //}}AFX_DATA_INIT m_strBmpFilePath = ""; ZeroMemory(&m_bmpIfHi, sizeof(BITMAPINFOHEADER)); m_dwSize = 0; m_lpDIBits = NULL; } void CAddTextDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAddTextDlg) DDX_Control(pDX, IDC_EDIT_TEXT, m_edtText); DDX_Control(pDX, IDC_STATIC_SHOW, m_BmpShow); DDX_Text(pDX, IDC_EDIT_TEXT, m_strText); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAddTextDlg, CDialog) //{{AFX_MSG_MAP(CAddTextDlg) ON_EN_CHANGE(IDC_EDIT_TEXT, OnChangeEditText) //}}AFX_MSG_MAP ON_BN_CLICKED(IDOK, OnBnClickedOk) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CAddTextDlg message handlers BOOL CAddTextDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here m_edtText.EnableWindow(FALSE); int nCharLimit = 0; if (m_strBmpFilePath.GetLength() > 0) {//传文件路径 if (m_BmpShow.SetReadBmpPath(m_strBmpFilePath, &nCharLimit)) { m_edtText.EnableWindow(); m_edtText.SetLimitText(nCharLimit); } else { PostMessage(WM_CLOSE, 0, 0); } } else {//传位图信息和数据 if (NULL == m_lpDIBits) { PostMessage(WM_CLOSE, 0, 0); } if (m_BmpShow.SetBmpInfo(&m_bmpIfHi, m_lpDIBits, &nCharLimit)) { m_edtText.EnableWindow(); m_edtText.SetLimitText(nCharLimit); } else { PostMessage(WM_CLOSE, 0, 0); } } return TRUE; // return TRUE unless you set the focus to a control } void CAddTextDlg::OnChangeEditText() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. UpdateData(); m_BmpShow.SetBmpText(m_strText); // TODO: Add your control notification handler code here } BOOL CAddTextDlg::SetBmpFilePath(LPCTSTR pszPath) { if (0 == _mbstrlen(pszPath)) { return FALSE; } m_strBmpFilePath = pszPath; return TRUE; } void CAddTextDlg::OnOK() { // TODO: Add extra validation here if (m_strBmpFilePath.GetLength() > 0) { m_BmpShow.SetSaveBmpPath(m_strBmpFilePath); } if (m_lpDIBits != NULL) { delete[] m_lpDIBits; m_lpDIBits = NULL; } m_lpDIBits = new BYTE[m_dwSize]; m_BmpShow.SaveModify(&m_bmpIfHi, m_lpDIBits); CDialog::OnOK(); } void CAddTextDlg::OnCancel() { // TODO: Add extra cleanup here CDialog::OnCancel(); } BOOL CAddTextDlg::SetBmpInfo(const LPBITMAPINFOHEADER lpbmih, const LPVOID lpvBits) { ASSERT((lpbmih != NULL) && (lpvBits != NULL)); m_strBmpFilePath = ""; if (m_lpDIBits != NULL) { delete[] m_lpDIBits; m_lpDIBits = NULL; } if ((lpbmih->biBitCount < 16) || (lpbmih->biBitCount > 32)) { return FALSE; } memcpy(&m_bmpIfHi, lpbmih, sizeof(BITMAPINFOHEADER)); m_BmpShow.ComputeImageSize(&m_bmpIfHi, &m_dwSize); /* if (m_dwSize != lpbmih->biSizeImage) { return FALSE; }*/ m_lpDIBits = new BYTE[m_dwSize]; memcpy(m_lpDIBits, lpvBits, lpbmih->biSizeImage); // memcpy(m_lpDIBits, lpvBits, m_dwSize); return TRUE; } CAddTextDlg::~CAddTextDlg() { if (m_lpDIBits != NULL) { delete[] m_lpDIBits; m_lpDIBits = NULL; } } BOOL CAddTextDlg::GetBmpInfo(LPBITMAPINFOHEADER lpbmih, LPVOID lpvBits, LPDWORD pdwSize) { memcpy(lpbmih, &m_bmpIfHi, sizeof(BITMAPINFOHEADER)); *pdwSize = m_dwSize; memcpy(lpvBits, m_lpDIBits, m_dwSize); return TRUE; } void CAddTextDlg::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 OnOK(); }
评论 11
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值