您的位置:首页 > 其它

一个能自由切换字符验证和简单数学运算验证方式的 验证码操作类

2011-04-22 19:04 495 查看
生成输出图形的代码是借鉴园子里童鞋的。

上图

点击查看代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.IO;

namespace TheCms.Common
{
/// <summary>
/// 验证码生成类
/// </summary>
public class Captcha
{
#region 属性
/// <summary>
/// 验证码长度
/// </summary>
private int _CodeLength=4;
/// <summary>
/// 验证码长度,Normal方式下才有用
/// </summary>
public int CodeLength { get { return _CodeLength; } set { _CodeLength = value; } }
/// <summary>
/// 验证码类型(Normal,普通数字+字母验证。Operation,运算符验证。)
/// </summary>
public ValidateType ValidateType { get; set; }

#endregion
#region 生成验证码
/// <summary>
/// 生成验证码
/// </summary>
public void CreateCaptch()
{
Bitmap BitImage;
BitImage=CreateImages(GenCode(),ValidateType);
BitImage.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);
//释放对象
BitImage.Dispose();
}
#endregion
#region 生成输出图片
/// <summary>
/// 画图片的背景图,干扰
/// </summary>
/// <param name="checkCode"></param>
/// <returns></returns>
private Bitmap CreateImages(string checkCode, ValidateType type)
{
int step = 0;
if (type == ValidateType.Operation)
{
step = 5;//运算字符比较大,所以字距要比较大
}
else { step = 3; }
int iwidth = (int)(checkCode.Length * (13 + step));
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 25);
Graphics g = Graphics.FromImage(image);

g.Clear(Color.White);//清除背景色

Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Brown, Color.DarkCyan, Color.Purple };//定义随机颜色

string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
Random rand = new Random();

for (int i = 0; i < 50; i++)
{
int x1 = rand.Next(image.Width);
int x2 = rand.Next(image.Width);
int y1 = rand.Next(image.Height);
int y2 = rand.Next(image.Height);
g.DrawLine(new Pen(Color.LightGray, 1), x1, y1, x2, y2);//根据坐标画线
}

for (int i = 0; i < checkCode.Length; i++)
{
int cindex = rand.Next(6);
int findex = rand.Next(5);

Font f = new System.Drawing.Font(font[findex], 13, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(c[cindex]);
int ii = 4;
if ((i + 1) % 2 == 0)
{
ii = 2;
}
g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * (12 + step)), ii);
}

g.DrawRectangle(new Pen(c[rand.Next(6)], 0), 0, 0, image.Width - 1, image.Height - 1);

System.IO.MemoryStream ms = new System.IO.MemoryStream();
return image;
}
#endregion
#region 验证验证码
/// <summary>
/// 比较用户输入验证码(True验证码准确,False验证码错误。)
/// </summary>
/// <param name="InputCheckCode">待验证的验证码</param>
/// <returns>True验证码准确,False验证码错误。</returns>
public static bool CheckVerifyCode(string InputCheckCode)
{
return System.Web.HttpContext.Current.Session["VerifyCode"].ToString().ToLower() == InputCheckCode.ToLower();
}
#endregion
#region GenCode
public string GenCode()
{
Random rd = new Random();
string sResult = "";
if (ValidateType == ValidateType.Normal)
{
//普通方式验证
string[] source ={"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"};

for (int i = 0; i < CodeLength; i++)
{
sResult += source[rd.Next(0, source.Length)];
}
System.Web.HttpContext.Current.Session["VerifyCode"] = sResult;
}
else
{
//运算符方式验证
int Num1 = rd.Next(10);
int Num2 = rd.Next(10);
int Result = 0;
int F = (rd.Next(3) + 1);
switch (F)
{
case 1:
Result = Num1 + Num2;
sResult=Num1+"+"+Num2+"=";
break;
case 2:
Result = Num1 * Num2;
sResult = Num1 + "×" + Num2 + "=";
break;
case 3:
if (Num1 < Num2)
{
Result = Num2 - Num1;
sResult = Num2 + "-" + Num1 + "=";
}
else {
Result = Num1 - Num2;
sResult = Num1 + "-" + Num2 + "=";
}
break;
default:
Result = Num1 + 9;
sResult = Num1 + "+9 =";
break;
}
System.Web.HttpContext.Current.Session["VerifyCode"] = Result.ToString();
}
return sResult;
}
#endregion
}
#region 验证方式
/// <summary>
/// 验证方式。
/// Normal,普通数字+字母验证。
/// Operation,运算符验证。
/// </summary>
public enum ValidateType
{
Normal = 1,
Operation = 2,
}
#endregion
}


调用方法。

TheCms.Common.Captcha _captcha = new TheCms.Common.Captcha();
_captcha.ValidateType = TheCms.Common.ValidateType.Operation;
_captcha.CodeLength = 4;//如果在普通方式下有用 运算符方式没用。
_captcha.CreateCaptch();


验证方法:

if (TheCms.Common.Captcha.CheckVerifyCode(TextBox1.Text))
{
Response.Write("对");
}
else { Response.Write("错"); }


高手别喷 纯属交流。

运算符 偷懒鸟。只写了 + - *
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐