功能说明:
1. 可自定义字体
2. 可自定义最大字号
3. 可自定义随机旋转的角度
4. 可自定义前景随机噪色量
5. 可外部生成随机码,由VerifyCode类处理成图片
6. 可自定义数字与英文字母随机出现的比率
7. 可自定义验证码长度
8. 可自定义验证码背景色
9. 可自定义验证码色彩(噪点色同验证码色一致)
功能说明:
1. 可自定义字体
2. 可自定义最大字号
3. 可自定义随机旋转的角度
4. 可自定义前景随机噪色量
5. 可外部生成随机码,由VerifyCode类处理成图片
6. 可自定义数字与英文字母随机出现的比率
7. 可自定义验证码长度
8. 可自定义验证码背景色
9. 可自定义验证码色彩(噪点色同验证码色一致)
下载DEMO源代码
附源代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ValidCode
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.IO;
using System.Drawing;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public partial class ValidCode : System.Web.UI.Page
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
VerifyCode vcode = new VerifyCode();
vcode.CreateImage(base.Context);
Session["VerifyCode"] = vcode.RandomCode;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class VerifyCode
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 验证码长度
/// </summary>
private int _length = 4;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 字体最大尺寸
/// </summary>
private int _fontSize = 18;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 边框,0时没有连框
/// </summary>
private int _border = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 背景色
/// </summary>
private Color _backgroundColor = Color.AliceBlue;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 验证码色
/// </summary>
private Color _fontColor = Color.Blue;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 验证码中的数字出现机率 ,越大出现的数字机率越大
/// </summary>
private int _rateNumber = 10;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 随机生成的验证码
/// </summary>
private string _randomChars;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 随机码的旋转角度
/// </summary>
private int _randomAngle = 40;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 字体
/// </summary>
private string _fontFamily = "Verdana";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 噪点数量,0 时不用
/// </summary>
private int _chaosNumber = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 随机种子,公用
/// </summary>
private Random random = new Random();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 噪点
/// </summary>
/// <param name="chaosNumber">噪点</param>
public VerifyCode(int chaosNumber)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_chaosNumber = chaosNumber;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 长度,噪点
/// </summary>
/// <param name="length">长度</param>
/// <param name="chaosNumber">噪点</param>
public VerifyCode(int length, int chaosNumber)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_length = length;
_chaosNumber = chaosNumber;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 长度,噪点,数字机率
/// </summary>
/// <param name="length">长度</param>
/// <param name="chaosNumber">噪点</param>
/// <param name="rate">数字机率越大,生成的随机码中数字占的比例越多</param>
public VerifyCode(int length, int chaosNumber, int rate)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_length = length;
_chaosNumber = chaosNumber;
_rateNumber = rate;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
验证码长度(默认4个)#region 验证码长度(默认4个)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 验证码长度(默认4个)
/// </summary>
public int Length
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _length; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _length = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
字体最大尺寸(默认18)#region 字体最大尺寸(默认18)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 字体最大尺寸(默认18)
/// </summary>
public int FontSize
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _fontSize; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _fontSize = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
边框(默认0 没有连框)#region 边框(默认0 没有连框)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 边框(默认0 没有连框)
/// </summary>
public int Border
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _border; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _border = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
自定义背景色(默认Color.AliceBlue)#region 自定义背景色(默认Color.AliceBlue)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 自定义背景色(默认Color.AliceBlue)
/// </summary>
public Color BackgroundColor
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _backgroundColor; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _backgroundColor = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
验证码色(默认Color.Blue)#region 验证码色(默认Color.Blue)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 验证码色(默认Color.Blue)
/// </summary>
public Color FontColor
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _fontColor; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _fontColor = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
随机生成的验证码#region 随机生成的验证码
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 随机生成的验证码
/// </summary>
public string RandomCode
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _randomChars; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _randomChars = value.ToUpper(); }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
验证码中的数字出现机率,越大出现的数字机率越大(默认10)#region 验证码中的数字出现机率,越大出现的数字机率越大(默认10)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 验证码中的数字出现机率
/// </summary>
public int RateNumber
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _rateNumber; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _rateNumber = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
随机码的旋转角度(默认40度)#region 随机码的旋转角度(默认40度)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 随机码的旋转角度(默认40度)
/// </summary>
public int RandomAngle
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _randomAngle; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _randomAngle = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
字体#region 字体
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 字体
/// </summary>
public string FontFamily
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _fontFamily; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _fontFamily = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
噪点数量(默认值为2)#region 噪点数量(默认值为2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 噪点数量(默认值为2)
/// </summary>
public int ChaosNumber
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _chaosNumber; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _chaosNumber = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 生成随机验证码
/// </summary>
private void CreateCode()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//有外部输入验证码时不用产生,否则随机生成
if (!string.IsNullOrEmpty(_randomChars))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ return; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
char code;
for (int i = 0; i < _length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int rand = random.Next();
if (rand % _rateNumber == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ code = (char)('A' + (char)(rand % 26)); }
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ code = (char)('0' + (char)(rand % 10)); }
_randomChars += code.ToString();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Response.Cookies.Add(new HttpCookie("ValidCode", _randomChars));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 背景噪点生成
/// </summary>
/// <param name="graph"></param>
private void CreateBackgroundChaos(Bitmap map, Graphics graph)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Pen blackPen = new Pen(Color.Azure, 0);
for (int i = 0; i < map.Width * 2; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int x = random.Next(map.Width);
int y = random.Next(map.Height);
graph.DrawRectangle(blackPen, x, y, 1, 1);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 前景色噪点
/// </summary>
/// <param name="map"></param>
private void CreaetForeChaos(Bitmap map)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for (int i = 0; i < map.Width * _chaosNumber; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int x = random.Next(map.Width);
int y = random.Next(map.Height);
//map.SetPixel(x, y, Color.FromArgb(random.Next(300)));
map.SetPixel(x, y, _fontColor);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 创建随机码图片
/// </summary>
/// <param name="context"></param>
public void CreateImage(HttpContext context)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CreateCode(); //创建验证码
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Bitmap map = new Bitmap((int)(_randomChars.Length * 15), 24); //创建图片背景
Graphics graph = Graphics.FromImage(map);
//graph.FillRectangle(new SolidBrush(Color.Black), 0, 0, map.Width+1, map.Height+1); //填充一个有背景的矩形
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//if (_border > 0) //画一个边框
//{
// graph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, map.Width - _border, map.Height - _border);
//}
//graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //模式
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
graph.Clear(_backgroundColor); //清除画面,填充背景
SolidBrush brush = new SolidBrush(_fontColor); //画笔
Point dot = new Point(12, 12);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//CreateBackgroundChaos(map,graph); //背景噪点生成
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CreaetForeChaos(map); //前景色噪点
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//文字距中
StringFormat format = new StringFormat(StringFormatFlags.NoClip);
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//验证码旋转,防止机器识别
char[] chars = _randomChars.ToCharArray(); //拆散字符串成单字符数组
for (int i = 0; i < chars.Length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Font fontstyle = new Font(_fontFamily, random.Next(_fontSize - 3, _fontSize), FontStyle.Regular); //字体样式
float angle = random.Next(-_randomAngle, _randomAngle); //转动的度数
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
graph.TranslateTransform(dot.X, dot.Y); //移动光标到指定位置
graph.RotateTransform(angle);
graph.DrawString(chars[i].ToString(), fontstyle, brush, -2, 2, format);
graph.RotateTransform(-angle); //转回去
graph.TranslateTransform(2, -dot.Y); //移动光标到指定位置
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//生成图片
MemoryStream ms = new MemoryStream();
map.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
context.Response.ClearContent();
context.Response.ContentType = "image/gif";
context.Response.BinaryWrite(ms.ToArray());
graph.Dispose();
map.Dispose();
}
}
}
源码作者:zhouyu
http://max.cszi.com