验证码

发布时间:2014-10-23 23:28:21
来源:分享查询网

先建立一个一般应用程序 using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Web; using System.Web.SessionState; namespace 论坛 {     /// <summary>     /// Yanzhengma 的摘要说明     /// </summary>     public class Yanzhengma : IHttpHandler, IRequiresSessionState     {         public void ProcessRequest(HttpContext context)         {             var Response = context.Response;             Response.ContentType = "image/jpeg"; //需要输出图象信息 要修改HTTP头             int size = 5;//验证码的长度             string authCode = GetRandomNumberString(size);// 获得验证码字符             //在生成验证码的地方将验证码放到Session中,这样在用的时候就能从Session中取出正确的验证码了             context.Session["AuthCode"] = authCode;             using (Bitmap theBitmap = CreateImage(authCode))// 获得验证码图片             {                 theBitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);             }             Response.End();         }         private string GetRandomNumberString(int codeCount)         {             string strChoice = "2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";             string[] strResult = strChoice.Split(new Char[] { ',' });             string strReturn = "";             Random rnd = new Random();             for (int i = 0; i < codeCount; i++)             {                 int j = rnd.Next(strResult.Length);//随机数不能大于数组的长度                 strReturn = strReturn + strResult[j].ToString();             }             return strReturn;         }         private Bitmap CreateImage(string str_AuthCode)         {             int width = str_AuthCode.Length * 13;             int height = 20;             Random rad = new Random();             Bitmap bmp = new Bitmap(width, height);             using (Graphics grp = Graphics.FromImage(bmp))             {                 grp.Clear(Color.White);//填充bmp的背景色                 grp.DrawRectangle(new Pen(Color.Red, 1), 0, 0, width - 1, height - 1);//绘制边框                 int num = width * height;                 for (int i = 0; i < num; i += 3)//在图片的指定坐标上画上有颜色的圆点                 {                     int x = rad.Next(width);                     int y = rad.Next(height);                     int r = rad.Next(255);                     int g = rad.Next(255);                     int b = rad.Next(255);                     Color c = Color.FromArgb(r, g, b);                     bmp.SetPixel(x, y, c);//在图片的指定坐标上画上有颜色的圆点                 }                 using (Font f = new Font("宋体", 12, FontStyle.Bold))//定义字体                 {                     using (Brush br = new SolidBrush(Color.Black))//定义画笔的颜色及字体的颜色                     {                         for (int i = 0; i < str_AuthCode.Length; i++)                         {                             string s = str_AuthCode.Substring(i, 1);//单个单个的将字画到图片上                             Point p = new Point(i * 12 + rad.Next(3), rad.Next(3) + 1);//字体出现的位置(坐标)                             grp.DrawString(s, f, br, p);//绘制字符串                         }                     }                 }                 return bmp;//返回             }         }         public bool IsReusable         {             get             {                 return false;             }         }     } } 前台页面   <script type="text/javascript">          function shuaxin() {              document.getElementById('img1').setAttribute('src', 'Yanzhengma.ashx?id=' + Math.random());          }     </script>     <tr> <td>验证码:</td> <td><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td></tr>                        <tr> <td colspan="2"> <img src="Yanzhengma.ashx" id="img1" onclick="shuaxin();" /><a href="#" onclick="shuaxin();">看不清,刷新一下</a> </td></tr>

返回顶部
查看电脑版