using System.Drawing;
using System.Web;
using System.Web.SessionState;
/// <summary>
/// CaptchaHandler 的摘要说明
/// </summary>
public class CaptchaHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
// GDI+ 三步 1画布 2为画布创建画笔 3绘制所需素材
var vCode = CaptchaHelper.CreateRandomCode(5); //自己封装的扩展方法
var buffer = CaptchaHelper.DrawImage(vCode, background: Color.White); //自己封装的扩展方法
context.Session["vCode"] = vCode;
context.Response.ContentType = "image/gif";
context.Response.BinaryWrite(buffer);
}
public bool IsReusable { get { return false; } }
}
【关键】Handler 要实现 IRequiresSessionState 接口(所在的命名空间 using System.Web.SessionState;)
时间: 2024-11-05 13:42:38