zoukankan      html  css  js  c++  java
  • 生成 验证验证码

    主页前端代码

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="bbb.aspx.cs" Inherits="bbb" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
    
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Image ID="Image1" ImageUrl="~/YZM.aspx" runat="server" /><br />验证码图片  ImageUrl="~/YZM.aspx"图片来自于根目录下的YZM.aspx
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><%--提示验证成功还是失败--%>
                <br />
                <asp:Button ID="Button1" runat="server" Text="验证" />
    
            </div>
        </form>
    </body>
    </html>
    <script type="text/javascript">
        var count = 0;
        //验证码图片的点击事件  (验证码看不清)
        document.getElementById("Image1").onclick = function () {
            //每次点击路径重新绑定一次
            this.src = "yzm.aspx?a=" + count;
            count++;
        }
    
    
    </script>

    右键查看代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class bbb : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += Button1_Click;
        }
    
        void Button1_Click(object sender, EventArgs e)
        {
            //获取用户输入的内容
            string s = TextBox1.Text;
            //将用户输入的内容全部转换成大写
            if (s.ToUpper() == Session["yzm"].ToString().ToUpper())
                Label1.Text = "正确!";
            else
                Label1.Text = "错误";
        }
    }

    生成验证码的web窗体
      不需要前台代码

      右键查看代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;//引用画画类的命名空间
    
    public partial class YZM : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //建立一个颜色的泛型集合
            List<Color> clist = new List<Color>();
            //将下列的颜色添加到上面的泛型集合了
            clist.Add(Color.Red);
            clist.Add(Color.Orange);
            clist.Add(Color.Pink);
            clist.Add(Color.Yellow);
            clist.Add(Color.Blue);
            clist.Add(Color.Aqua);
            clist.Add(Color.Green);
            clist.Add(Color.SeaGreen);
            clist.Add(Color.Orchid);
            //建立一个画布宽100高50   Bitmap像素固定 
            Bitmap img = new Bitmap(100, 50);
            //建立一个画布 将img放到画布上
            Graphics g = Graphics.FromImage(img);
            //建立一个随机对象
            Random r = new Random();
            // 画布背景颜色  往画布填充矩形(实线画刷全部填满颜色从clist随机生成,)矩形的大小从左上角0,0到右下角100,50
            g.FillRectangle(new SolidBrush(clist[r.Next(0, clist.Count)]), 0, 0, 100, 50);
            //往画布添加直线 for循环一次自动生成10条直线(放到验证码前面先生成直线后生成验证码验证码会把直线盖住易看清 )
            for (int i = 0; i < 10; i++)
            {
                //建立画笔  画笔颜色随机生成  粗细从1到5随机生成
                Pen pe = new Pen(new SolidBrush(clist[r.Next(0, clist.Count)]), r.Next(1, 5));
                //直线起始的点  横向从0开始到100(画布长100)随机生成  纵向从0到50(画布宽50)随机生成 
                Point startP = new Point(r.Next(0, 100), r.Next(0, 50));
                //直线结束的点  横向从0开始到100(画布长100)随机生成  纵向从0到50(画布宽50)随机生成 
                Point endP = new Point(r.Next(0, 100), r.Next(0, 50));
                //将生成的画笔 起始点 结束点放到画布里
                g.DrawLine(pe, startP, endP);
            }
    
            //验证码
            string s = "";
            //验证码的全部内容
            string all = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
            //for循环验证码全部内容
            for (int i = 0; i < 4; i++)
            {
                //验证码的全部内容里从索引0到all的长度开始随机截取一个数一共4个数
                s += all.Substring(r.Next(0, all.Length), 1);
            }
            //往画布添加内容 四个参数 内容s  黑体30px  字体颜色从clist里随机生成  从左上角0,0,开始
            g.DrawString(s, new Font("黑体", 30), new SolidBrush(clist[r.Next(0, clist.Count)]), new PointF(0, 0));
    
            Session["yzm"] = s;
            //生成的图片输出                 Response.OutputStream属性 输出流
            img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    
        }
    }

    效果

  • 相关阅读:
    Android Architecture Components
    adb命令
    Dagger2 scope
    Dagger2学习资源
    Dependency Injection学习笔记
    什么是ADB
    使用AndroidStudio dump heap,再用 Eclipse MAT插件分析内存泄露
    Dagger学习笔记
    linux & shell & nginx & Docker Kubernetes
    Go 目录
  • 原文地址:https://www.cnblogs.com/skyhorseyk/p/7471034.html
Copyright © 2011-2022 走看看