zl程序教程

您现在的位置是:首页 >  其他

当前栏目

验证码测试

测试 验证码
2023-09-11 14:14:14 时间

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()"为了单机图片时,能刷新验证码图片