ChaosSea验证码0.20版
2008-03-28 11:51 咒语 阅读(886) 评论(2) 编辑 收藏 举报
演示地址:http://max.cszi.com/VerifyCode.aspx
下载地址:源文件:
点击下载文件
ChaosSea验证码0.20版
功能说明:
1. 可自定义字体
2. 可自定义最大字号
3. 可自定义随机旋转的角度
4. 可自定义前景随机噪色量
5. 可外部生成随机码,由VerifyCode类处理成图片
6. 可自定义数字与英文字母随机出现的比率
7. 可自定义验证码长度
8. 可自定义验证码背景色
9. 可自定义验证码色彩(噪点色同验证码色一致)
10. 可自定义字体
------------------------------
本人觉得用上随机角度就行了,不用前景噪点,因为这个东东会耗一定的资源
1using System;
2using System.Collections.Generic;
3using System.Web;
4using System.Web.UI;
5using System.Web.UI.WebControls;
6using System.Web.UI.WebControls.WebParts;
7using System.Web.UI.HtmlControls;
8
9using System.IO;
10using System.Drawing;
11
12
13namespace CS.Common.Utility
14{
15 /// <summary>
16 /// Chaos验证码
17 /// FileName : VerifyCode.cs
18 /// Verion : 0.20
19 /// Author : zhouyu http://max.cszi.com
20 /// Update : 2007-10-10
21 /// Description : 验证码随机旋转一定角度,可使用前景色,背景色效果不大就不用了
22 /// </summary>
23 public class VerifyCode
24 {
25
26 private int _length = 4; //验证码长度
27 private int _fontSize = 18; //字体最大尺寸
28 private int _border = 0; //边框,0时没有连框
29 private Color _backgroundColor = Color.AliceBlue; //背景色
30 private Color _fontColor = Color.Blue; //验证码色
31 private int _rateNumber = 10; //验证码中的数字出现机率 ,越大出现的数字机率越大
32 private string _randomChars; //随机生成的验证码
33 private int _randomAngle = 40; //随机码的旋转角度
34 private string _fontFamily = "Verdana"; //字体
35 private int _chaosNumber = 0; //噪点数量 ,0 时不用
36
37 private Random random = new Random(); //随机种子,公用
38
39 public VerifyCode()
40 {
41
42 }
43
44 /// <summary>
45 /// 重载一 :噪点
46 /// </summary>
47 /// <param name="chaosNumber"></param>
48 public VerifyCode(int chaosNumber)
49 {
50 _chaosNumber = chaosNumber;
51 }
52
53 /// <summary>
54 /// 重载二:长度,噪点
55 /// </summary>
56 /// <param name="length"></param>
57 /// <param name="chaosNumber"></param>
58 public VerifyCode(int length, int chaosNumber)
59 {
60 _length = length;
61 _chaosNumber = chaosNumber;
62 }
63
64 /// <summary>
65 /// 重载三:长度,噪点,数字机率
66 /// </summary>
67 /// <param name="length"></param>
68 /// <param name="chaosNumber"></param>
69 /// <param name="rate">越大,生成的随机码中数字占的比例越多</param>
70 public VerifyCode(int length, int chaosNumber, int rate)
71 {
72 _length = length;
73 _chaosNumber = chaosNumber;
74 _rateNumber = rate;
75 }
76
77
78 .Length 验证码长度(默认4个)
85
86 .FontSize 字体最大尺寸(默认18)
93
94 .Border 边框(默认0 没有连框)
101
102 .BackgroundColor 自定义背景色(默认Color.AliceBlue)
109
110 .FontColor 验证码色(默认Color.Blue)
117
118 .RandomCode 随机生成的验证码
125
126 .RateNumber 验证码中的数字出现机率,越大出现的数字机率越大(默认10)
133
134 .RandomAngle 随机码的旋转角度(默认40度)
141
142 .FontFamily 字体
149
150 .ChaosNumber 噪点数量(默认值为2)
157
158
159
160 /// <summary>
161 /// 生成随机验证码
162 /// </summary>
163 private void CreateCode()
164 {
165 //有外部输入验证码时不用产生,否则随机生成
166 if (!string.IsNullOrEmpty(_randomChars))
167 { return; }
168
169 char code;
170 for (int i = 0; i < _length; i++)
171 {
172 int rand = random.Next();
173 if (rand % _rateNumber == 0)
174 { code = (char)('A' + (char)(rand % 26)); }
175 else
176 { code = (char)('0' + (char)(rand % 10)); }
177 _randomChars += code.ToString();
178 }
179 }
180
181
182 /// <summary>
183 /// 背景噪点生成
184 /// </summary>
185 /// <param name="graph"></param>
186 private void CreateBackgroundChaos(Bitmap map,Graphics graph)
187 {
188 Pen blackPen = new Pen(Color.Azure, 0);
189 for (int i = 0; i < map.Width * 2; i++)
190 {
191 int x = random.Next(map.Width);
192 int y = random.Next(map.Height);
193 graph.DrawRectangle(blackPen, x, y, 1, 1);
194 }
195 }
196
197 /// <summary>
198 /// 前景色噪点
199 /// </summary>
200 /// <param name="map"></param>
201 private void CreaetForeChaos(Bitmap map)
202 {
203 for (int i = 0; i < map.Width * _chaosNumber; i++)
204 {
205 int x = random.Next(map.Width);
206 int y = random.Next(map.Height);
207 //map.SetPixel(x, y, Color.FromArgb(random.Next(300)));
208 map.SetPixel(x, y, _fontColor);
209 }
210 }
211
212
213 /// <summary>
214 /// 创建随机码图片
215 /// </summary>
216 /// <param name="context"></param>
217 public void CreateImage(HttpContext context)
218 {
219 CreateCode(); //创建验证码
220
221 Bitmap map = new Bitmap((int)(_randomChars.Length * 15), 24); //创建图片背景
222 Graphics graph = Graphics.FromImage(map);
223 //graph.FillRectangle(new SolidBrush(Color.Black), 0, 0, map.Width+1, map.Height+1); //填充一个有背景的矩形
224
225 //if (_border > 0) //画一个边框
226 //{
227 // graph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, map.Width - _border, map.Height - _border);
228 //}
229 //graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //模式
230
231 graph.Clear(_backgroundColor); //清除画面,填充背景
232 SolidBrush brush = new SolidBrush(_fontColor); //画笔
233 Point dot = new Point(12, 12);
234
235 //CreateBackgroundChaos(map,graph); //背景噪点生成
236
237 CreaetForeChaos(map); //前景色噪点
238
239 //文字距中
240 StringFormat format = new StringFormat(StringFormatFlags.NoClip);
241 format.Alignment = StringAlignment.Center;
242 format.LineAlignment = StringAlignment.Center;
243
244 //验证码旋转,防止机器识别
245 char[] chars = _randomChars.ToCharArray(); //拆散字符串成单字符数组
246 for (int i = 0; i < chars.Length; i++)
247 {
248 Font fontstyle = new Font(_fontFamily, random.Next(_fontSize - 3, _fontSize), FontStyle.Regular); //字体样式
249 float angle = random.Next(-_randomAngle, _randomAngle); //转动的度数
250
251 graph.TranslateTransform(dot.X, dot.Y); //移动光标到指定位置
252 graph.RotateTransform(angle);
253 graph.DrawString(chars[i].ToString(), fontstyle, brush, -2, 2, format);
254 graph.RotateTransform(-angle); //转回去
255 graph.TranslateTransform(2, -dot.Y); //移动光标到指定位置
256 }
257
258
259 //生成图片
260 MemoryStream ms = new MemoryStream();
261 map.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
262 context.Response.ClearContent();
263 context.Response.ContentType = "image/gif";
264 context.Response.BinaryWrite(ms.ToArray());
265 graph.Dispose();
266 map.Dispose();
267
268 }
269
270
271
272
273
274 }
275
276
277
278}
279
2using System.Collections.Generic;
3using System.Web;
4using System.Web.UI;
5using System.Web.UI.WebControls;
6using System.Web.UI.WebControls.WebParts;
7using System.Web.UI.HtmlControls;
8
9using System.IO;
10using System.Drawing;
11
12
13namespace CS.Common.Utility
14{
15 /// <summary>
16 /// Chaos验证码
17 /// FileName : VerifyCode.cs
18 /// Verion : 0.20
19 /// Author : zhouyu http://max.cszi.com
20 /// Update : 2007-10-10
21 /// Description : 验证码随机旋转一定角度,可使用前景色,背景色效果不大就不用了
22 /// </summary>
23 public class VerifyCode
24 {
25
26 private int _length = 4; //验证码长度
27 private int _fontSize = 18; //字体最大尺寸
28 private int _border = 0; //边框,0时没有连框
29 private Color _backgroundColor = Color.AliceBlue; //背景色
30 private Color _fontColor = Color.Blue; //验证码色
31 private int _rateNumber = 10; //验证码中的数字出现机率 ,越大出现的数字机率越大
32 private string _randomChars; //随机生成的验证码
33 private int _randomAngle = 40; //随机码的旋转角度
34 private string _fontFamily = "Verdana"; //字体
35 private int _chaosNumber = 0; //噪点数量 ,0 时不用
36
37 private Random random = new Random(); //随机种子,公用
38
39 public VerifyCode()
40 {
41
42 }
43
44 /// <summary>
45 /// 重载一 :噪点
46 /// </summary>
47 /// <param name="chaosNumber"></param>
48 public VerifyCode(int chaosNumber)
49 {
50 _chaosNumber = chaosNumber;
51 }
52
53 /// <summary>
54 /// 重载二:长度,噪点
55 /// </summary>
56 /// <param name="length"></param>
57 /// <param name="chaosNumber"></param>
58 public VerifyCode(int length, int chaosNumber)
59 {
60 _length = length;
61 _chaosNumber = chaosNumber;
62 }
63
64 /// <summary>
65 /// 重载三:长度,噪点,数字机率
66 /// </summary>
67 /// <param name="length"></param>
68 /// <param name="chaosNumber"></param>
69 /// <param name="rate">越大,生成的随机码中数字占的比例越多</param>
70 public VerifyCode(int length, int chaosNumber, int rate)
71 {
72 _length = length;
73 _chaosNumber = chaosNumber;
74 _rateNumber = rate;
75 }
76
77
78 .Length 验证码长度(默认4个)
85
86 .FontSize 字体最大尺寸(默认18)
93
94 .Border 边框(默认0 没有连框)
101
102 .BackgroundColor 自定义背景色(默认Color.AliceBlue)
109
110 .FontColor 验证码色(默认Color.Blue)
117
118 .RandomCode 随机生成的验证码
125
126 .RateNumber 验证码中的数字出现机率,越大出现的数字机率越大(默认10)
133
134 .RandomAngle 随机码的旋转角度(默认40度)
141
142 .FontFamily 字体
149
150 .ChaosNumber 噪点数量(默认值为2)
157
158
159
160 /// <summary>
161 /// 生成随机验证码
162 /// </summary>
163 private void CreateCode()
164 {
165 //有外部输入验证码时不用产生,否则随机生成
166 if (!string.IsNullOrEmpty(_randomChars))
167 { return; }
168
169 char code;
170 for (int i = 0; i < _length; i++)
171 {
172 int rand = random.Next();
173 if (rand % _rateNumber == 0)
174 { code = (char)('A' + (char)(rand % 26)); }
175 else
176 { code = (char)('0' + (char)(rand % 10)); }
177 _randomChars += code.ToString();
178 }
179 }
180
181
182 /// <summary>
183 /// 背景噪点生成
184 /// </summary>
185 /// <param name="graph"></param>
186 private void CreateBackgroundChaos(Bitmap map,Graphics graph)
187 {
188 Pen blackPen = new Pen(Color.Azure, 0);
189 for (int i = 0; i < map.Width * 2; i++)
190 {
191 int x = random.Next(map.Width);
192 int y = random.Next(map.Height);
193 graph.DrawRectangle(blackPen, x, y, 1, 1);
194 }
195 }
196
197 /// <summary>
198 /// 前景色噪点
199 /// </summary>
200 /// <param name="map"></param>
201 private void CreaetForeChaos(Bitmap map)
202 {
203 for (int i = 0; i < map.Width * _chaosNumber; i++)
204 {
205 int x = random.Next(map.Width);
206 int y = random.Next(map.Height);
207 //map.SetPixel(x, y, Color.FromArgb(random.Next(300)));
208 map.SetPixel(x, y, _fontColor);
209 }
210 }
211
212
213 /// <summary>
214 /// 创建随机码图片
215 /// </summary>
216 /// <param name="context"></param>
217 public void CreateImage(HttpContext context)
218 {
219 CreateCode(); //创建验证码
220
221 Bitmap map = new Bitmap((int)(_randomChars.Length * 15), 24); //创建图片背景
222 Graphics graph = Graphics.FromImage(map);
223 //graph.FillRectangle(new SolidBrush(Color.Black), 0, 0, map.Width+1, map.Height+1); //填充一个有背景的矩形
224
225 //if (_border > 0) //画一个边框
226 //{
227 // graph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, map.Width - _border, map.Height - _border);
228 //}
229 //graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //模式
230
231 graph.Clear(_backgroundColor); //清除画面,填充背景
232 SolidBrush brush = new SolidBrush(_fontColor); //画笔
233 Point dot = new Point(12, 12);
234
235 //CreateBackgroundChaos(map,graph); //背景噪点生成
236
237 CreaetForeChaos(map); //前景色噪点
238
239 //文字距中
240 StringFormat format = new StringFormat(StringFormatFlags.NoClip);
241 format.Alignment = StringAlignment.Center;
242 format.LineAlignment = StringAlignment.Center;
243
244 //验证码旋转,防止机器识别
245 char[] chars = _randomChars.ToCharArray(); //拆散字符串成单字符数组
246 for (int i = 0; i < chars.Length; i++)
247 {
248 Font fontstyle = new Font(_fontFamily, random.Next(_fontSize - 3, _fontSize), FontStyle.Regular); //字体样式
249 float angle = random.Next(-_randomAngle, _randomAngle); //转动的度数
250
251 graph.TranslateTransform(dot.X, dot.Y); //移动光标到指定位置
252 graph.RotateTransform(angle);
253 graph.DrawString(chars[i].ToString(), fontstyle, brush, -2, 2, format);
254 graph.RotateTransform(-angle); //转回去
255 graph.TranslateTransform(2, -dot.Y); //移动光标到指定位置
256 }
257
258
259 //生成图片
260 MemoryStream ms = new MemoryStream();
261 map.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
262 context.Response.ClearContent();
263 context.Response.ContentType = "image/gif";
264 context.Response.BinaryWrite(ms.ToArray());
265 graph.Dispose();
266 map.Dispose();
267
268 }
269
270
271
272
273
274 }
275
276
277
278}
279
使用方法:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CS.Common.Utility;
namespace Test
{
public partial class Vcode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
VerifyCode vcode = new VerifyCode();
vcode.CreateImage(base.Context);
Session["VerifyCode"] = vcode.RandomCode;
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CS.Common.Utility;
namespace Test
{
public partial class Vcode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
VerifyCode vcode = new VerifyCode();
vcode.CreateImage(base.Context);
Session["VerifyCode"] = vcode.RandomCode;
}
}
}
下载地址:源文件:
点击下载文件