• 验证码


    首先 制作一个验证码界面    .aspx

    后台代码

     protected void Page_Load(object sender, EventArgs e)
        {
            List<Color> clist = new List<Color>();
            clist.Add(Color.Red);
            clist.Add(Color.Firebrick);
            clist.Add(Color.LawnGreen);
            clist.Add(Color.Goldenrod);
            clist.Add(Color.Cyan);
            clist.Add(Color.DarkSlateBlue);
            clist.Add(Color.Indigo);
            Random r = new Random();
            string s = "";
            string all = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmeopqrstuvwxyz0123456789";
            for (int i = 0; i < 4; i++)
            {
                s += all.Substring(r.Next(0, all.Length), 1);
            }
    
            Session["YZM"] = s;
    
            Bitmap img = new Bitmap(120, 60);
    
            Graphics g2 = Graphics.FromImage(img);
            Brush b2 = new SolidBrush(clist[r.Next(0, clist.Count)]);
            g2.FillRectangle(b2, 0, 0, 120, 60);
    
            Graphics g = Graphics.FromImage(img);
            Font f = new Font("微软雅黑", 30);
            Brush b = new SolidBrush(Color.Red);
    
            g.DrawString(s, f, b, new PointF(0, 0));
    
    
            for (int i = 0; i < 8; i++)
            {
                Graphics g3 = Graphics.FromImage(img);
                Pen p3 = new Pen(new SolidBrush(clist[r.Next(0, clist.Count)]), r.Next(2, 5));
                g3.DrawLine(p3, new Point(r.Next(0, 120), r.Next(0, 60)), new Point(r.Next(0, 120), r.Next(0, 60)));
            }
    
    
            img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
    
    
    
        }
    View Code

    要使用的界面    .aspx

    使用的控件

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <img id="yzm1" src="YZM.aspx" /><br />
    <asp:Button ID="Button1" runat="server" Text="验证" />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

    JS 代码

    <script type="text/javascript">
    var a = 0;
    document.getElementById("yzm1").onclick = function () {
    this.src = "yzm.aspx?a=" + a;
    a++;
    }

    </script>

    后台空内容对错

     Button1.Click += Button1_Click;


    void Button1_Click(object sender, EventArgs e)
    {
    Label2.Text = Session["YZM"].ToString();
    if (TextBox1.Text == Session["YZM"].ToString())
    Label1.Text = "正确!!!";
    else
    Label1.Text = "错误!!!!!!!";
    }

  • 相关阅读:
    Python必须知道的异常处理
    类的内置方法(用实际代码来验证)
    类的三大特性(继承, 封装, 多态)
    面向对象编程代码详解(依赖关系,关联关系,组合关系)
    函数(匿名函数,嵌套函数,高阶函数,装饰器)
    常用模块
    对磁盘文件的操作(文件处理)
    字符编码
    常用数据类型
    编程介绍
  • 原文地址:https://www.cnblogs.com/zhangwei99com/p/7004432.html
Copyright © 2020-2023  润新知