验证码生成处理页面:CheckCode.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace ASP.Net中级
{
/// <summary>
/// CheckCode 的摘要说明
/// </summary>
public class CheckCode : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/JPEG";
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(100, 50))
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
//通过随机数来动态生成验证码
Random rand = new Random();
int code = rand.Next(10000, 99999);
string strCode = Convert.ToString(code);
HttpContext.Current.Session["code"] = strCode;
g.DrawString(strCode, new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Green, new System.Drawing.PointF(0, 0));
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}