首页 > 代码库 > 验证码测试

验证码测试

1 调用

<img src="http://www.mamicode.com/Handler1.ashx" onclick="this.src=http://www.mamicode.com/‘Handler1.ashx?aa=‘+new Date()" />

 

2.生成图片的Handler1.ashx

  public class Handler1 : IHttpHandler,IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/JPEG";
            using (Bitmap bp = new Bitmap(100, 50))
            {
                using (Graphics g = Graphics.FromImage(bp))
                {
                    Random r = new Random();
                    int code = r.Next(1000, 9999);
                    string strCode = code.ToString();
                    HttpContext.Current.Session["code"]=strCode;
                    g.DrawString(strCode,new System.Drawing.Font("宋体",12),Brushes.Green,new System.Drawing.PointF(0,0));
                    bp.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
        }

   注意:a.IRequiresSessionState必须要实现这个接口,为了调用 HttpContext.Current.Session["code"]=strCode

    b.aa=‘+new Date()"为了单机图片时,能刷新验证码图片

验证码测试