电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> C#>>Asp.net实现字符串转换为图片(附视频地址):

Asp.net实现字符串转换为图片(附视频地址)

来源:杨明波-博客 | 2009-8-4 | (有4402人读过)

asp.net字符串转换为图片的相关代码**:

subject.ashx代码:

<%@ WebHandler Language="C#" Class="subject" %>

using System;
using System.Web;
using System.Drawing;
using System.IO;

public class subject : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        ConvertStringToImage(context, "字符串转换为图片", 500, 40, Color.FromArgb(0, 86, 152));
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

    private void ConvertStringToImage(HttpContext context, string outputString, int width, int height, Color color)
    {
        Bitmap image = new Bitmap(width, height);
        Graphics g = Graphics.FromImage(image);
        Font font = new Font("SimHei", 24, FontStyle.Bold);
        SolidBrush brush = new SolidBrush(color);
        g.Clear(Color.White);
        StringFormat format = new StringFormat();
        g.DrawString(outputString,font, brush, 0, 0, format);
        MemoryStream ms = new MemoryStream();
        image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
        context.Response.ClearContent();
        context.Response.ContentType = "image/pjpeg";
        context.Response.BinaryWrite(ms.ToArray());
    }

}

stringToImage.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="stringToImage.aspx.cs" Inherits="stringToImage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <img src=subject.ashx />
    </div>
    </form>
</body>
</html>

视频demo地址:
http://download.cnblogs.com/insus/ASPDOTNET/SubjectToImage.zip

出自微软mvp杨明波的blog。

C#热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号