码迷,mamicode.com
首页 > 其他好文 > 详细

验证码测试

时间:2014-09-05 09:53:41      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:http   os   io   for   sp   on   c   new   ef   

1 调用

<img src="Handler1.ashx" onclick="this.src=‘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()"为了单机图片时,能刷新验证码图片

验证码测试

标签:http   os   io   for   sp   on   c   new   ef   

原文地址:http://www.cnblogs.com/ChineseMoonGod/p/3957270.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!