zoukankan      html  css  js  c++  java
  • 图片验证码制作

    default.aspx里:包含点击图片更新的js代码

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
    
    <!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" runat="server" ImageUrl="YZM.aspx" />
            <asp:Button ID="Button1" runat="server" Text="Button" />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
        </form>
    </body>
    </html>
    <script>//点击图片更新
        var a = 0;
        document.getElementById("Image1").onclick = function ()
        {
            this.setAttribute("src","yzm.aspx?id="+a);
            a++;
        }
    
    
    </script>

    default.cs里:验证相同的按钮事件写在这里

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    
    public partial class Default3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += Button1_Click;
        }
    
        void Button1_Click(object sender, EventArgs e)//验证是否相同
        {
            string t1 = TextBox1.Text;
            string t2 = Session["yzm"].ToString();
            if (t1.ToUpper() == t2.ToUpper())
            {
                Label1.Text = "验证成功";
            }
            else
            { Label1.Text = "验证失败"; }
        }
    }

    YZM.aspx.cs里:主要的绘制,生成写在这里

    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)
        {
            Bitmap img = new Bitmap(70, 30);
            Graphics g = Graphics.FromImage(img);//绘制
            Random r = new Random();
    
            List<Color> clist = new List<Color>();//设定颜色集合
            clist.Add(Color.Yellow);
            clist.Add(Color.Black);
            clist.Add(Color.Blue);
            clist.Add(Color.Green);
            clist.Add(Color.Pink);
            clist.Add(Color.Orange);
    
            for (int i = 0; i < 10; i++)
            {
                Color cl = clist[r.Next(0, clist.Count)];//随机选取颜色
                Pen pe = new Pen(new SolidBrush(cl),r.Next(1,5));//随机生成颜色,随机生成线的粗细
                g.DrawLine(pe, new PointF(r.Next(0, 70), r.Next(0, 30)), new PointF(r.Next(0, 70), r.Next(0, 30)));//随机生成线
            }
    
            string all = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";//设定验证码集合
            string s = "";
            
            for (int i = 0; i < 4; i++)//随机生成四位验证码
            {
                s += all.Substring(r.Next(1,all.Length),1);
            }
            Session["yzm"] = s;//设置全局变量,用于验证调用
            
    
           
    
            g.DrawString(s, new Font("微软雅黑", 15), new SolidBrush(Color.Red), new PointF(5, 5));//绘制验证码
    
            img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);//输出流
          
         response.end(); } }
  • 相关阅读:
    openjudge666:放苹果—题解
    欢迎来到路由器的世界!这里是开端
    Codevs1169:传纸条——题解
    POJ3498:March of the Penguins——题解
    模板:并查集
    模板:快速幂
    模板:排序(三)
    程序员之间的鄙视链
    自动化测试的成本高效果差,那么自动化测试的意义在哪呢
    自动化测试的成本高效果差,那么自动化测试的意义在哪呢
  • 原文地址:https://www.cnblogs.com/wy1992/p/6259750.html
Copyright © 2011-2022 走看看