图片验证码

Posted 庚xiao午

tags:

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

<%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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:Label ID="Label1" runat="server" Text="" ForeColor="Red"></asp:Label>
        <br /><br />
        &nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Image ID="Image1" runat="server" ImageUrl="YZM.aspx" />
        <br />
        <asp:Button ID="Button1" runat="server" Text="提交" />
    </div>
    </form>
</body>
</html>
<script type="text/javascript">
    var i = 1;
    document.getElementById("Image1").onclick = function () {
        this.setAttribute("src", "YZM.aspx?"+i);
        i++;
    };
</script>
页面html代码
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)
    {
        Random r = new Random();

        //画布
        Bitmap img = new Bitmap(100, 50);
        Graphics g = Graphics.FromImage(img);

        //背景色
        List<Color> clist = new List<Color>();
        clist.Add(Color.Gray);
        g.FillRectangle(new SolidBrush(clist[r.Next(0, clist.Count)]), 0, 0, 100, 50);

        //干扰线
        Pen p=new Pen(new SolidBrush(Color.WhiteSmoke),1);
        for (int i = 0; i < 8; i++)
        {
            g.DrawLine(p, r.Next(0, 100), r.Next(0, 50), r.Next(0, 100), r.Next(0, 50));
        }

        //内容
        string s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        string str="";                                                                                      
        for (int i = 0; i < 4; i++)                                                                    
        {
            str += s.Substring(r.Next(0, s.Length), 1);
        }

        //全局变量,验证码内容
        Session["YZM"] = str;

        //字体
        Font f = new Font("黑体", 30);

        //画笔、颜色
        Brush b = new SolidBrush(Color.WhiteSmoke);

        //绘画、设置位置
        g.DrawString(str, f, b, 2, 2);

        //输出图片
        img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        Response.End();     
    }
}
验证码页面C#代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;    //提交按钮点击事件
    }

    //提交按钮点击事件
    void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text.ToUpper() != Session["YZM"].ToString().ToUpper())
        {
            Label1.Text = "验证码错误!";
            TextBox1.Text = "";
        }
        else 
        { 
            Label1.Text = "";
        }
    }
}
后台C#代码

 

 

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

java 登陆时的验证码怎么做?

纯代码系列:Python实现验证码图片(PIL库经典用法用法,爬虫12306思路)

随机验证码图片验证码和邮箱发送用户验证码

web 动态随机验证码图片生成最新

第二十三节:scrapy爬虫识别验证码图片验证码识别

图片识别之验证码识别