C# 完美验证码的实例代码
本文导语: 一、函数代码 CheckCode.ASpx 代码示例: //添加引用: using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Text; /// /// 创建验证码,返回验证码字符串 /// /// int CodeLe:验证码长度 /// int CodeType:0纯...
一、函数代码 CheckCode.ASpx
//添加引用:
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;
///
/// 创建验证码,返回验证码字符串
///
/// int CodeLe:验证码长度
/// int CodeType:0纯数字;1纯字母;2数字与字母混合;3纯汉字
///
///
private string CreateCheckCode(int CodeLen, int CodeType)
{
/**参数说明:CodeLen(验证码长度);CodeType(0纯数字,1纯字母,2,数字与字母混合,3,纯汉字)**/
if (CodeType > 3)
{
CodeType = 3;
}
string codestring = "";
//定义验证图片的长度与宽度
int Clen=30*CodeLen, Cheight=40;
//定义验证图片的背景颜色
Color CBcolor = Color .FromArgb(200,200,200);
string[] font = { "Bell MT", "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体","幼圆", "楷体_GB2312","仿宋_GB2312" };
Font Cfont = new Font("Arial", 20, FontStyle.Bold);
//产生随机验证码
switch (CodeType)
{
//纯数字
case 0:
Random random1 = new Random();
codestring = random1.Next((int)System.Math.Pow(10,CodeLen-1),(int) System.Math.Pow(10,CodeLen)-1).ToString();
break;
//纯数字
//纯字母
case 1:
string Vchar1 = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
String[] VcArray1 = Vchar1.Split(',');
int vclen1 = VcArray1.Length;
int vcindex1;
Random random2 = new Random();
for (int i = 0; i < CodeLen; i++)
{
vcindex1 = random2.Next(vclen1);
codestring = codestring + VcArray1[vcindex1];
}
break;
case 2:
string Vchar2 = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
String[] VcArray2 = Vchar2.Split(',');
int vclen2 = VcArray2.Length;
int vcindex2;
Random random3 = new Random();
for (int i = 0; i < CodeLen; i++)
{
vcindex2 = random3.Next(vclen2);
codestring = codestring + VcArray2[vcindex2];
}
break;
//纯字母
//纯汉字
case 3:
//定义一个字符串数组储存汉字编码的组成元素
string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
Random rnd = new Random();
int strlength = CodeLen*2;
//定义一个object数组用来
object[] bytes = new object[strlength];
/**/
/*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中
每个汉字有四个区位码组成
区位码第1位和区位码第2位作为字节数组第一个元素
区位码第3位和区位码第4位作为字节数组第二个元素
*/
for (int i = 0; i < strlength; i++)
{
//区位码第1位
int r1 = rnd.Next(11, 14);
string str_r1 = rBase[r1].Trim();
//区位码第2位
rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的种子避免产生重复值
int r2;
if (r1 == 13)
{
r2 = rnd.Next(0, 7);
}
else
{
r2 = rnd.Next(0, 16);
}
string str_r2 = rBase[r2].Trim();
//区位码第3位
rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
int r3 = rnd.Next(10, 16);
string str_r3 = rBase[r3].Trim();
//区位码第4位
rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
int r4;
if (r3 == 10)
{
r4 = rnd.Next(1, 16);
}
else if (r3 == 15)
{
r4 = rnd.Next(0, 15);
}
else
{
r4 = rnd.Next(0, 16);
}
string str_r4 = rBase[r4].Trim();
//定义两个字节变量存储产生的随机汉字区位码
byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
//将两个字节变量存储在字节数组中
byte[] str_r = new byte[] { byte1, byte2 };
//将产生的一个汉字的字节数组放入object数组中
bytes.SetValue(str_r, i);
}
Encoding gb = Encoding.GetEncoding("gb2312");
//根据汉字编码的字节数组解码出中文汉字
for(int i=0;i