验证码类(转)
这些东西网上都泛滥了,随便拉了个过来,然后改改,加上自己的标示什么的,就是这样子了。
可以定义验证码的格式,包括字母、数字、特殊符号和汉字,指定生成的图片大小。
本来还想加上图片扭曲的,可怕以后用起来被人说“万恶的验证码”所以就算了。
可以定义验证码的格式,包括字母、数字、特殊符号和汉字,指定生成的图片大小。
本来还想加上图片扭曲的,可怕以后用起来被人说“万恶的验证码”所以就算了。
1
using System;
2
using System.Collections;
3
using System.Collections.Generic;
4
using System.Drawing;
5
using System.Text;
6
using System.Threading;
7
using System.Drawing.Drawing2D;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
namespace SHNK.Framework.Utility
10
{
11
/// <summary>
12
/// 验证码生成类
13
/// </summary>
14
public static class ValidateCodeHelper
15
{
16
/// <summary>
17
/// 生成长度为length、字符类型为codeType的验证码字符串
18
/// </summary>
19
/// <param name="codeType">验证码类型</param>
20
/// <param name="length">验证码长度</param>
21
/// <returns>验证码字符串</returns>
22
public static string GenerateCode(ValidateCodeType codeType, int length)
23
{
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
const string sNumber = "0123456789";
26
const string sLetter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
27
const string sSymbol = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
if ((codeType | ValidateCodeType.Chinese) == codeType)
30
{
31
return GetChinese(length);
32
}
33![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
StringBuilder blocks = new StringBuilder();
35
StringBuilder builder = new StringBuilder();
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
if ((codeType | ValidateCodeType.Number) == codeType)
38
blocks.Append(sNumber);
39![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
if ((codeType | ValidateCodeType.Letter) == codeType)
41
blocks.Append(sLetter);
42![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
if ((codeType | ValidateCodeType.Symbol) == codeType)
44
blocks.Append(sSymbol);
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
Random rand = new Random();
47
for (int i = 0; i < length; i++)
48
{
49
builder.Append(blocks[rand.Next(blocks.Length)]);
50
}
51![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
return builder.ToString();
53
}
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
/// <summary>
56
/// 获取num个中文字符
57
/// </summary>
58
/// <param name="num">字符串长度</param>
59
/// <returns>中文字符</returns>
60
private static string GetChinese(int num)
61
{
62
/* *********************************************
63
* be in common use
64
*
65
* first byte 0xB0 - 0xD7
66
* second byte 0xA1 - 0xFE
67
*
68
* except 0xD7FA - 0xD7FE (space)
69
* *********************************************/
70
const int _MAX_SIZE = 3755; // (0xD7 - 0xB0 + 1) * (0xFE - 0xA1 + 1) - (0xD7FE - 0xD7FA + 1);
71
const int _Capacity = 94; // 0xFE - 0xA1 + 1
72![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
Random rand = new Random();
74
StringBuilder builder = new StringBuilder();
75
for (int i = 0; i < num; i++)
76
{
77
int index = rand.Next(_MAX_SIZE);
78
int high = Math.Floor(index.ToDouble() / _Capacity.ToDouble()).ToInt32();
79
int lower = index % _Capacity;
80
string code = Encoding.Default.GetString(new byte[] { (byte)(high + 0xB0), (byte)(lower + 0xA1) });
81
builder.Append(code);
82
}
83
return builder.ToString();
84
}
85![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
/// <summary>
87
/// 按照输入的字符串src和每个字符位图的宽度生成一张图片
88
/// </summary>
89
/// <param name="src">验证码</param>
90
/// <param name="height">图片高度</param>
91
/// <param name="wdpc">每个字符所占宽度</param>
92
/// <returns>验证图片</returns>
93
public static Image GenerateImage(string src, int height, int wdpc)
94
{
95
Bitmap img = new Bitmap(src.Length * wdpc, height);
96
Graphics g = Graphics.FromImage(img);
97
g.Clear(Color.White);
98![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
Random rand = new Random();
100
for (int i = 0; i < 25; i++)
101
{
102
int x1 = rand.Next(img.Width);
103
int x2 = rand.Next(img.Width);
104
int y1 = rand.Next(img.Height);
105
int y2 = rand.Next(img.Height);
106![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
108
}
109
Font f = new Font("'Segoe UI' 微软雅黑 Verdana 宋体", 14f);
110
Brush b = new LinearGradientBrush(new Rectangle(new Point(0, 0), img.Size), Color.Blue, Color.DarkRed, LinearGradientMode.ForwardDiagonal);
111
for (int i = 0; i < src.Length; i++)
112
g.DrawString(src[i].ToString(), f, b, wdpc * i.ToSingle(), 0f);
113![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
114
g.Save();
115
for (int i = 0; i < 100; i++)
116
{
117
int x = rand.Next(img.Width);
118
int y = rand.Next(img.Height);
119![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
120
img.SetPixel(x, y, Color.FromArgb(rand.Next()));
121
}
122![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
123
return img;
124
}
125
}
126![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
127
ValidateCodeType Enum
168![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
169
public static partial class Extension
170
{
171
convert method
187
}
188
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
92
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
93
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
95
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
100
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
102
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
104
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
106
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
109
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
111
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
112
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
113
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
114
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
115
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
116
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
117
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
118
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
119
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
120
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
121
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
122
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
123
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
124
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
125
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
126
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
127
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
168
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
169
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
170
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
171
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
187
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
188
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
成功者找方法,失败者找借口!