H ttpHandler实现动态绘制图片

•HttpHandler是对请求的响应,可以输出普通的html内容,也可以输出图片、也可以输出一个文件(下载)<那么如何判断输出的文件类型?比如输出的是MP3还是.jpeg、png-->使用HTTP协议的contentType属性来判断>
•输出一幅动态创建的图片(能看懂就可以)
 
本例实现在已有图片的指定位置画一个字符串:
 1 <%@ WebHandler Language="C#" Class="MakeImg" %>
 2 
 3 using System;
 4 using System.Web;
 5 using System.Drawing;
 6 
 7 public class MakeImg : IHttpHandler {
 8     
 9     public void ProcessRequest (HttpContext context) {
10         //context.Response.ContentType = "text/plain";
11         //context.Response.Write("Hello World");
12         
13         //设置回传内容的类型
14         context.Response.ContentType = "image/jpeg";
15         using (Image img = Image.FromFile(context.Server.MapPath("Image\\00_05.jpg")))
16         {
17             Graphics g = Graphics.FromImage(img);
18             g.DrawString("我是yaoxinchao", new Font("宋体", 16), Brushes.Blue, new Point(0, 0));
19             //img.Save(context.Server.MapPath("Image\\1.jpeg", 
20             img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
21         }
22     }
23  
24     public bool IsReusable {
25         get {
26             return false;
27         }
28     }
29 
30 }
View Code
•案例1:图片中显示访问者信息
 
•案例2:填入朋友的姓名就能生成恶搞的图片链接
•案例3:绘制图片水印
•练习:制作简单验证码图片
–网上看到的注册、登录时候的验证码也是动态生成的图片、55.la也是这样实现的原理。
 
posted @ 2013-05-28 18:44  Big.Eagle  阅读(358)  评论(0编辑  收藏  举报