一个好的验证码生成类
2008-03-04 13:47
183 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Drawing;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Drawing.Imaging;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Drawing.Drawing2D;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace Bll
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 生成验证码的类
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class ValidateNumber
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public ValidateNumber()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 验证码的最大长度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int MaxLength
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
get ...{ return 10; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 验证码的最小长度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int MinLength
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
get ...{ return 1; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 生成验证码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// <param name="length">指定验证码的长度</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <returns></returns>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string CreateValidateNumber(int length)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int[] randMembers = new int[length];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int[] validateNums = new int[length];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string validateNumberStr = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//生成起始序列值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int seekSeek = unchecked((int)DateTime.Now.Ticks);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Random seekRand = new Random(seekSeek);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int beginSeek = (int)seekRand.Next(0, Int32.MaxValue - length * 10000);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int[] seeks = new int[length];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < length; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beginSeek += 10000;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
seeks[i] = beginSeek;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//生成随机数字
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < length; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Random rand = new Random(seeks[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int pownum = 1 * (int)Math.Pow(10, length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
randMembers[i] = rand.Next(pownum, Int32.MaxValue);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//抽取随机数字
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < length; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string numStr = randMembers[i].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int numLength = numStr.Length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Random rand = new Random();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int numPosition = rand.Next(0, numLength - 1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//生成验证码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < length; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
validateNumberStr += validateNums[i].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return validateNumberStr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 创建验证码的图片
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// <param name="containsPage">要输出到的page对象</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <param name="validateNum">验证码</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void CreateValidateGraphic(Page containsPage, string validateNum)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Bitmap image = new Bitmap((int)Math.Ceiling(validateNum.Length * 12.5), 22);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Graphics g = Graphics.FromImage(image);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//生成随机生成器
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Random random = new Random();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//清空图片背景色
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.Clear(Color.White);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//画图片的干扰线
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < 25; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int x1 = random.Next(image.Width);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int x2 = random.Next(image.Width);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int y1 = random.Next(image.Height);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int y2 = random.Next(image.Height);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Color.Blue, Color.DarkRed, 1.2f, true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.DrawString(validateNum, font, brush, 3, 2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//画图片的前景干扰点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < 100; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int x = random.Next(image.Width);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int y = random.Next(image.Height);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
image.SetPixel(x, y, Color.FromArgb(random.Next()));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//画图片的边框线
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//保存图片数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MemoryStream stream = new MemoryStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
image.Save(stream, ImageFormat.Jpeg);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//输出图片
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
containsPage.Response.Clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
containsPage.Response.ContentType = "image/jpeg";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
containsPage.Response.BinaryWrite(stream.ToArray());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
finally
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.Dispose();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
image.Dispose();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 得到验证码图片的长度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// <param name="validateNumLength">验证码的长度</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <returns></returns>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static int GetImageWidth(int validateNumLength)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return (int)(validateNumLength * 12.5);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 得到验证码的高度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <returns></returns>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static double GetImageHeight()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 22.5;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- NET 很简单的一个生成随机验证码封装的一个类
- 封装了一个类,可生成验证码,缩略图,及水印图
- 一个python的验证码生成类
- 在JSP中动态生成随机验证码,登录时后台校验验证码,以及如何避免同一个验证码被重复提交爆破密码
- 生成一个包含数字和字母的自定义位数的验证码
- paip.提升安全性-------生成一个安全的验证码
- PHP生成一个简单的验证码图片
- ASP中一个很不错的四位数字验证码生成代码 - [ASP程序]
- NET 很简单的一个生成随机验证码封装的一个类
- 一个简单的验证码生成工具--servlet版本
- 写一个含数字,拼音,汉字的验证码生成类
- 分享给大家一个生成图形验证码的View
- C#生成一个复杂的验证码、背景随机、字体随机、中英文数字随机
- js随机生成一个数组中的随机字符串以及更新验证码
- 转 一个简单实用的 生成验证码的 代码
- QTP如何设置动态属性的标准检查点? 此业务是:点击【生成验证码】按钮,会在一个文本框中生成验证码
- ASPX验证码生成程序(C#代码一个页面实现)
- 请问一下在xml注册界面做一个框,并且把代码生成的验证码显示到里面,谢谢了
- 设计一个函数,根据指定长度生成对应的验证码(由数字和大小写英文字母构成的随机字符串)
- 写一个含数字,拼音,汉字的验证码生成类