ASP.NET生成验证码(C#)

       做网站那么久了,总感觉自己最先做的验证码有点难看。于是在网上找了很多资料,自己改进了一下,还可以吧!

      首先是写一个生成验证码的类:

     public class Validate
{
 public Validate()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
 }
    public void CreateValidate(Page containsPage)
    {
        string checkCode = this.GetCode(CodeLength);
        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((double)(5 * 17)), 27);//画布
        Graphics g = Graphics.FromImage(image);

        try
        {
            Random random = new Random();
            g.Clear(Color.White); //图片背景色

            /**/
            ///画图片的背景噪音线
            for (int i = 0; i < 5; i++)
            {
                int x1 = random.Next(image.Width);
                int x2 = random.Next(image.Width);
                int y1 = random.Next(image.Height);
                int y2 = random.Next(image.Height);
                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }

            Font font = new System.Drawing.Font("Arial", 14, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.White, Color.White, 1.2f, true);

            /**/
            ///生成不同颜色字符的图片
            for (Int32 i = 0; i < 5; i++)
            {

                Brush b = new System.Drawing.SolidBrush(Color.FromArgb(random.Next(160), random.Next(160), random.Next(160)));
                Int32 ii = 4;
                if ((i + 1) % 2 == 0)
                {
                    ii = 2;
                }
                g.DrawString(checkCode.Substring(i, 1), font, b, 2 + (i * 14), ii);
            }

            /**/
            ///画图片的前景噪音点
            for (int i = 0; i < 20; i++)
            {
                int x = random.Next(image.Width);
                int y = random.Next(image.Height);

                image.SetPixel(x, y, Color.FromArgb(random.Next()));
            }

            /**/
            ///画图片的边框线
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            containsPage.Response.ClearContent();
            containsPage.Response.ContentType = "image/Gif";
            containsPage.Response.BinaryWrite(ms.ToArray());
        }
        finally
        {
            g.Dispose();
            image.Dispose();
        }
    }
    public String GetCode(Int32 length)
    {
        String[] source ={"2","3","4","5","6","7","8","9",
            "a","b","c","d","e","f","g","h","i","j","k","m","n","p","q","r","s","t","u","v","w","x","y","z",
            "A","B","C","D","E","F","G","H","I","J","K","M","N","P","Q","R","S","T","U","V","W","X","Y","Z"};

//因为数字0,1和字母O,i不好认,所以我没有用到这几个!
        String code = "";
        Random rd = new Random();
        for (Int32 i = 0; i < length; i++)
        {
            code += source[rd.Next(0, source.Length)];
        }
        return code;
    }
    private System.Int32 CodeLength
    {
        get
        {
            System.Int32 length;
            if (Int32.TryParse(HttpContext.Current.Request.QueryString["length"], out length))
            {
                if (length > 5)
                {
                    return length;
                }
            }
            return 5;
        }
    }
}

第二步就是新建一个用于显示验证码的Validate.aspx页面,在它的代码CS文件的pagelod事件中写入:

protected void Page_Load(object sender, EventArgs e)
    {
        Validate va = new Validate();
        Session["validate"] =va.GetCode(5);
       
        va.CreateValidate(this);
      
    }

   这样一来,就可以在你想使用的它的页面里放一个<img alt="" src="Validate.aspx"/>,OK了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值