验证码在登录还有一些注册页面是用的最多的,先写在这,以后少不了用到验证码
新建一个ashx的页子,这个是全部的代码,
在使用写好的验证码代码时,经常出现的问题就是不知道怎么调用,
就是下面的这句话<img src="验证码.ashx" οnclick="this.src='验证码.ashx?aaa='+new Date()"/>
在哪个页面中调用就把名改成那个页面的名字就行了。
<%@ WebHandler Language="C#" class="验证码" %>
using System;using System.Web;
public class 验证码 : IHttpHandler,System.Web.SessionState.IRequiresSessionState { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "image/JPEG"; //string path = context.Server.MapPath("/images/无标题.png");/这个地方注释去掉就可以换成一张图片了 using (System.Drawing.Bitmap bitmip = new System.Drawing.Bitmap(200,50)) 然后这个是跟上面连在一起的,去掉注释,把200,,50换成path就行了 { //创建一个图片的画布 using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmip)) { Random rand=new Random(); int code=rand.Next(); string strCode=code.ToString(); context.Session["Code"] = strCode; g.DrawString(strCode, new System.Drawing.Font("宋体", 20), System .Drawing.Brushes.Red, new System.Drawing.PointF(0,0)); bitmip.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); } } } public bool IsReusable { get { return false; } }
}
转载于:https://www.cnblogs.com/qzc900809/archive/2012/12/26/2834544.html