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。