asp.net2.0生成一个复杂的验证码,背景随机,字体随机,中英文数字随机

来源:互联网 发布:贵州大学网络课程登录 编辑:程序博客网 时间:2024/05/16 12:21

1、建立网站,添加一个网页ValitionNo.aspx,和Default.aspx,新建一个文件夹images。如图所示:
2、在images文件夹里添加以下5张图片:bg_0.jpg,bg_1.jpg,bg_2.jpg,bg_3.jpg,bg_4.jpg bg_0.jpg

 bg_1.jpg

 bg_2.jpg

 bg_3.jpg

 bg_4.jpg
3、ValitionNo.aspx里不用放任何控件,代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ValitionNo.aspx.cs" Inherits="guestBook_ValitionNo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
    
</div>
    
</form>
</body>
</html>


4、双击页面ValitionNo.aspx进入ValitionNo.aspx.cs页面,按如下所示填充代码:

using System;
using
 System.Data;
using
 System.Configuration;
using
 System.Collections;
using
 System.Web;
using
 System.Web.Security;
using
 System.Web.UI;
using
 System.Web.UI.WebControls;
using
 System.Web.UI.WebControls.WebParts;
using
 System.Web.UI.HtmlControls;
using
 System.Drawing.Imaging;
using
 System.Drawing.Drawing2D;
using
 System.Drawing;

public partial class
 guestBook_ValitionNo : System.Web.UI.Page
{
    
protected void Page_Load(object
 sender, EventArgs e)
    {
        Random rd 
= new Random(); //
创建随机数对象       

        
//以下4行,产生由6个字母和数字组成的一个字符串

        string str = "ABCDEFGHIJKLMNOPQRSTURWXYZ0123456789想象是无限的";
        
string yanzheng = ""
;
        
for (int i = 0; i < 6; i++
)
        {
            yanzheng 
= yanzheng + str.Substring(rd.Next(42), 1
);
        }
        
//验证码值存放到Session中用来比较

        Session["yanzheng"= yanzheng;

        
//以下三句,通过随机找一个现有图象产生一个画布Bitmap

        string bgFilePath = Server.MapPath(".//images//bg_" + new Random().Next(5+ ".jpg");//随机找个图象
        System.Drawing.Image imgObj = System.Drawing.Image.FromFile(bgFilePath);
        Bitmap newBitmap 
= new Bitmap(imgObj, 29080);//建立位图对象


        Graphics g 
= Graphics.FromImage(newBitmap);//根据上面创建的位图对象创建绘图面
        SolidBrush brush = new SolidBrush(Color.Blue);//设置画笔颜色

        
//定义一个含10种字体的数组

        String[] ziti ="Arial""Verdana""Comic Sans MS""Impact""Haettenschweiler""Lucida Sans Unicode""Garamond""Courier New""Book Antiqua""Arial Narrow" };

        
//通过循环,绘制每个字符,

        for (int a = 0; a < yanzheng.Length; a++)
        {
            Font textFont 
= new Font(ziti[rd.Next(9)], 30, FontStyle.Bold);//
字体随机,字号大小30,加粗 
            
//每次循环绘制一个字符,设置字体格式,画笔颜色,字符相对画布的X坐标,字符相对画布的Y坐标

            g.DrawString(yanzheng.Substring(a, 1), textFont, brush, 40 + a * 3620);
        }

        
//保存画的图片到输出流中

        newBitmap.Save(Response.OutputStream, ImageFormat.Gif);

    }
}


5、在Default.aspx里添加一个文本框,图片按钮,Label标签和普通按钮,ImageButton的图片路径是~/ValitionNo.aspx,而不是某张图片。代码如下:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        验证码:
<asp:TextBox ID="TextBox1" runat="server" Width="117px"></asp:TextBox>
        
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/ValitionNo.aspx" />
        
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False">换一张</asp:LinkButton><br />
        
<asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label><br />
        
<asp:Button ID="Button2" runat="server" Text="确定" OnClick="Button2_Click" /></div>
    
</form>
</body>
</html>


6、在Default.aspx.cs里代码如下:

using System;
using
 System.Data;
using
 System.Configuration;
using
 System.Web;
using
 System.Web.Security;
using
 System.Web.UI;
using
 System.Web.UI.WebControls;
using
 System.Web.UI.WebControls.WebParts;
using
 System.Web.UI.HtmlControls;

public partial class
 _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object
 sender, EventArgs e)
    {

    }
    
protected void Button2_Click(object
 sender, EventArgs e)
    {
        
if (Session["yanzheng"!= null && TextBox1.Text != Session["yanzheng"
].ToString())
        {
            Label1.Text 
= "验证码错误!"
;
            
return
;
        }
        
else

        {
            Label1.Text 
= "验证码正确!";
        }
    }
}


7、运行效果如下所示:



如果点验证码图片或者点换一张按钮,都可以使验证码随机改变,背景随机改变,数字字母汉字随机改变。