图形验证码的实现

  • 作用:防止恶意注册,恶意破解密码,恶意灌水
  • 分类:客户端验证和服务器端验证。
  • 关键问题:1,获得一个随机生成的字符串  2,把字符串画到网页上。  3.比较验证

网上的代码很多,自己也动手模仿了一个。

  1. 获得一个随机的字符串

 #region  随机取得数字字母
        private string GenarateCheckCode()
        {
            int number;
            char code;
            string checkCode = string.Empty;
            System.Random random = new Random();

            for (int i = 0; i <= 5; i++)
            {
                number = random.Next();
                if (number % 2 == 0)
                    code = (char)('0' + number % 10);
                else
                    code=(char)('A'+number%26);
                checkCode += code.ToString();

            }
            Session["num"] = checkCode;
            return checkCode;
        }
        #endregion

2.把字符串画到网页上

   #region 把随机数字和字母画到图片上
        private void createCodeCheckImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == string.Empty)
                return;
            System.Drawing.Bitmap img = new Bitmap((Int32)Math.Ceiling(checkCode.Length*12.5),22);
            System.Drawing.Graphics g = Graphics.FromImage(img);
            try {
            Random random=new Random();
                g.Clear(Color.White);

                for(int i=0;i<25;i++)
                {
                     int x1=random.Next(img.Width);
                     int x2=random.Next(img.Width);
                     int y1=random.Next(img.Height);
                     int y2=random.Next(img.Height);
                    g.DrawLine(new Pen(Color.Silver),x1,y1,x2,y2);
                }

                Font font=new Font("Arial",12,FontStyle.Bold);
                System.Drawing.Drawing2D.LinearGradientBrush brush=new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0,0,img.Width,img.Height),Color.Blue,Color.Red,1.2f,true);
                g.DrawString(checkCode,font,brush,2,2);

                for(int i=0;i<10;i++)
                {
                   int x =random.Next(img.Width);
                    int y =random.Next(img.Height);
                    img.SetPixel(x,y,Color.FromArgb(random.Next()));
                }
                g.DrawRectangle(new Pen(Color.Silver),0,0,img.Width-1,img.Height-1);
                System.IO.MemoryStream ms=new System.IO.MemoryStream();
                img.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
                Response.ClearContent();
                Response.ContentType="image/Gif";
                Response.BinaryWrite(ms.ToArray());

            }
            finally{
                g.Dispose();
                img.Dispose();
            }

        }
        #endregion

3.验证

private void checkCode() 

{
            string vnum = Session["num"].ToString();
            Session.Abandon();
            ViewState["num"] = vnum;
            if (this.TextBox1.Text == ViewState["num"].ToString())
            {
                Response.Write("<script>alert('验证码正确!')</script>");
                return;
            }
            else
            {
                Response.Write("<script>alert('验证码错误!')</script>");
                return;
            }
        }

理解了这三个关键点,我想,图形验证码的编写也可以自己胜任了。感谢网友的分享。

你可能感兴趣的:(验证码)