using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Drawing.Drawing2D;
using System.Drawing;
namespace AspTechnicalBase
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class DrawingOnWebPage : IHttpHandler
{ /// <summary>
/// 绘图的相关参数
/// </summary>
private const int ImageWidth = 600;
private const int ImageHeight = 100;
/// <summary>
/// 响应HTTP请求
/// </summary>
/// <param name="context">HTTP请求的上下文对象</param>
public void ProcessRequest(HttpContext context)
{
//开辟一个ImageWidth像素宽、ImageHeight像素高的绘图缓冲区
Bitmap bmp = new Bitmap(ImageWidth, ImageHeight);
//创建绘图表面对象,引用这一绘图缓冲区
Graphics g = Graphics.FromImage(bmp);
//生成一个渐变的画刷
Brush br = new LinearGradientBrush(new Point(0, 0), new Point(ImageWidth, 0), Color.Black, Color.Blue);
//用渐变画刷填充绘图表面
g.FillRectangle(br, g.ClipBounds);
string str = "使用GDI+动态生成Web图像";
Font f = new Font("宋体", 30);
//输出文字
g.DrawString(str, f, Brushes.Yellow, new PointF(40, 20));
//将结果输出到浏览器
context.Response.ContentType = "Image/JPEG";
context.Response.Clear();
context.Response.BufferOutput = true;
bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
//释放相关的绘图对象
br.Dispose();
f.Dispose();
g.Dispose();
bmp.Dispose();
}
/// <summary>
/// 是否自动缓存此对象以供下次复用
/// </summary>
public bool IsReusable
{
get
{
return false;
}
}
}
}
使用:
<form id="form1" runat="server">
<div>
<h2>使用一般处理程序绘制Web图像</h2>
<hr />
<img src="DrawingOnWebPage.ashx" alt="动态生成的GDI+图像" />
</div>
</form>