很棒的验证码(可以任意设置相关属性)(汉字,英文,数字等)

来源:互联网 发布:在中国的 外国人知乎 编辑:程序博客网 时间:2024/05/17 01:33
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Drawing;

namespace VerifyColorCode
{
    
public partial class VerifyCode : System.Web.UI.Page
    
{
        
public Encoding GB = Encoding.GetEncoding("GB2312");
        
protected void Page_Load(object sender, EventArgs e)
        
{

            VerifyCode v 
= new VerifyCode();

            v.Length 
= this.length;
            v.FontSize 
= this.fontSize;
            v.Chaos 
= this.chaos;
            v.BackgroundColor 
= this.backgroundColor;
            v.ChaosColor 
= this.chaosColor;
            v.CodeSerial 
= this.codeSerial;
            v.Colors 
= this.colors;
            v.Fonts 
= this.fonts;
            v.Padding 
= this.padding;
            
string code = v.CreateVerifyCode();                //取随机码
            v.CreateImageOnPage(code, this.Context);        // 输出图片
            Response.Cookies.Add(new HttpCookie("CheckCode", code.ToUpper()));// 使用Cookies取验证码的值

        }


        
#region 验证码长度(默认6个验证码的长度)
        
int length = 6;
        
public int Length
        
{
            
get return length; }
            
set { length = value; }
        }

        
#endregion


        
#region 验证码字体大小(为了显示扭曲效果,默认40像素,可以自行修改)
        
int fontSize = 12;
        
public int FontSize
        
{
            
get return fontSize; }
            
set { fontSize = value; }
        }

        
#endregion


        
#region 边框补(默认1像素)
        
int padding = 1;
        
public int Padding
        
{
            
get return padding; }
            
set { padding = value; }
        }

        
#endregion


        
是否输出燥点(默认不输出)

        
输出燥点的颜色(默认灰色)

        
自定义背景色(默认白色)

        
自定义随机颜色数组

        
自定义字体数组

        
自定义随机码字符串序列(使用逗号分隔)

        
产生波形滤镜效果

        
生成校验码图片

        
将创建好的图片输出到页面

        
生成随机字符码

        
    }

}

 

 

前台页面:

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

<!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>




调用方法:
<img id="imgVerify" src="VerifyCode.aspx?" alt="看不清?点击更换" onclick="this.src=this.src+'?'" />

后台方法:

     if (Request.Cookies["CheckCode"] == null)
        {
            Alert(this, "您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。");
            return;
        }

        if (String.Compare(Request.Cookies["CheckCode"].Value, txtV.Text.ToString().Trim(), true) != 0)
        {
            Alert(this,"验证码错误!");
            return;
        }