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

     界面:
    1
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> 2 3 <!DOCTYPE html> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 8 <title></title> 9 </head> 10 <body> 11 <form id="form1" runat="server"> 12 <div> 13 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 14 <asp:Image ID="Image1" runat="server" ImageUrl="YZM.aspx" /> 15 <asp:Button ID="Button1" runat="server" Text="验证" /> 16 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 17 </div> 18 </form> 19 </body> 20 </html> 21 <script type="text/javascript"> 22 var a = 0; 23 //点击图片更换验证码 24 document.getElementById("Image1").onclick = function () { 25 this.setAttribute("src", "yzm.aspx?id=" + a); 26 a++; 27 } 28 </script>

    后台:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.UI;
     6 using System.Web.UI.WebControls;
     7 
     8 public partial class Default2 : System.Web.UI.Page
     9 {
    10     protected void Page_Load(object sender, EventArgs e)
    11     {
    12         Button1.Click += Button1_Click;
    13     }
    14 
    15     void Button1_Click(object sender, EventArgs e)
    16     {
    17         string t1 = TextBox1.Text;
    18         string t2 = Session["YZM"].ToString();
    19 
    20         if (t1.ToUpper() == t2.ToUpper())
    21         {
    22             Label1.Text = "验证成功!";
    23         }
    24         else
    25         {
    26             Label1.Text = "验证失败!";
    27         }
    28 
    29 
    30     }
    31 }

    验证码制作:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.UI;
     6 using System.Web.UI.WebControls;
     7 using System.Drawing;
     8 
     9 public partial class YZM : System.Web.UI.Page
    10 {
    11     protected void Page_Load(object sender, EventArgs e)
    12     {
    13         Bitmap img = new Bitmap(80, 40);//验证码图片大小
    14         Graphics g = Graphics.FromImage(img);
    15         Random r = new Random();
    16         //背景色
    17         List<Color> clist = new List<Color>();
    18         clist.Add(Color.Yellow);
    19         clist.Add(Color.Green);
    20         clist.Add(Color.Blue);
    21         clist.Add(Color.Pink);
    22         clist.Add(Color.Orange);
    23         clist.Add(Color.AliceBlue);
    24         clist.Add(Color.Aqua);
    25         clist.Add(Color.Cyan);
    26         clist.Add(Color.DarkOliveGreen);
    27         clist.Add(Color.Black);
    28 
    29         g.FillRectangle(new SolidBrush(clist[r.Next(0, clist.Count)]), 0, 0, 80, 40);
    30 
    31 
    32         
    33         for (int i = 0; i < 10; i++)
    34         {
    35             Color ccc = clist[r.Next(0, clist.Count)];//随机颜色
    36 
    37             Pen ppp = new Pen(new SolidBrush(ccc), r.Next(1, 5));//随机线条
    38 
    39             g.DrawLine(ppp, new PointF(r.Next(0, 80), r.Next(0, 40)), new PointF(r.Next(0, 80), r.Next(0, 40)));
    40         }
    41 
    42 
    43 
    44 
    45         //验证码
    46         string all = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    47         string s = "";
    48 
    49         for (int i = 0; i < 4; i++)
    50         {
    51             int a = r.Next(all.Length);//随机取数
    52             s += all.Substring(a, 1);
    53         }
    54 
    55         Session["YZM"] = s;
    56 
    57         g.DrawString(s, new Font("微软雅黑", 16), new SolidBrush(Color.Red), new PointF(3, 3));
    58 
    59         img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
    60         Response.End();
    61 
    62     }
    63 }
  • 相关阅读:
    数据分析三剑客 numpy,oandas,matplotlib(2)
    数据分析三剑客 numpy,oandas,matplotlib
    爬虫 crawlSpider 分布式 增量式 提高效率
    RestTemplate:带 header 的 get 请求
    Java:md5 摘要
    Java:Base64 编码
    Java:将字符串转换为枚举
    RestTemplate:使用 Map 传递参数
    Spring cloud:网关-Zuul路由
    Spring cloud:熔断器-Hystrix Dashboard
  • 原文地址:https://www.cnblogs.com/maxin991025-/p/6256623.html
Copyright © 2011-2022 走看看