首页 > 代码库 > 图片验证码

图片验证码

技术分享
<%@ 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#代码

 

技术分享技术分享技术分享

 

图片验证码