验证码制作

Posted 丶蘇Da同

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了验证码制作相关的知识,希望对你有一定的参考价值。

Bitmap bit = new Bitmap(90, 40);//画布大小
        Graphics g = Graphics.FromImage(bit);//创建绘制对象,告诉它往哪张图片上绘制
        Random r = new Random();
        string s = "";
        Color color1 = Color.FromArgb(r.Next(155, 255), r.Next(155, 255), r.Next(155, 255));
        g.FillRectangle(new SolidBrush(color1), 0, 0, 90, 40);//画布颜色随机
        for (int i = 0; i < 10; i++)//随机画干扰线
        {
            Color color3 = Color.FromArgb(r.Next(170, 255), r.Next(190, 255), r.Next(170, 255));
            Pen pp = new Pen(new SolidBrush(color3), r.Next(0, 5));
            g.DrawLine(pp, r.Next(0, 90), r.Next(0, 40), r.Next(0, 90), r.Next(0, 40));
        }
        

        string yan = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";//设置随机的验证码,从里面抽取
        int wei = 0;
        for (int i = 0; i < 4; i++)//取四位验证码,颜色随机
        {
            string m = yan.Substring(r.Next(0, yan.Length), 1);
            s += m;
            Color color2 = Color.FromArgb(r.Next(0, 155), r.Next(0, 155), r.Next(0, 155));
            int w = r.Next(20, 25);
            Font f = new Font("黑体", w);
            SolidBrush b = new SolidBrush(color2);

            g.DrawString(m, f, b, wei, r.Next(0, 10));
            wei += w;
        }

        Session["YZM"] = s;//把验证码保存到session中
        for (int i = 0; i < 251; i++)
        {
            Color color3 = Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255));
            Pen pp = new Pen(new SolidBrush(color3), r.Next(0, 5));
            int a = r.Next(0, 90);
            int b = r.Next(0, 40);
            g.DrawLine(pp, a, b, (a + 1), (b + 1));
        }


        bit.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        Response.End();
验证码:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <asp:Image ID="Image1" runat="server" ImageUrl="Default2.aspx" />//这里图片路径不是一张图片的话会默认打开

 

以上是关于验证码制作的主要内容,如果未能解决你的问题,请参考以下文章

php验证码制作

验证码的制作和验证

php生成各种验证码

JSP 设计教师与学生不同登陆界面(带验证码)

Java验证码的jar制作

图片验证码制作