这些东西网上都泛滥了,随便拉了个过来,然后改改,加上自己的标示什么的,就是这样子了。
可以定义验证码的格式,包括字母、数字、特殊符号和汉字,指定生成的图片大小。
本来还想加上图片扭曲的,可怕以后用起来被人说“万恶的验证码”所以就算了。
可以定义验证码的格式,包括字母、数字、特殊符号和汉字,指定生成的图片大小。
本来还想加上图片扭曲的,可怕以后用起来被人说“万恶的验证码”所以就算了。
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
9
namespace SHNK.Framework.Utility
10

/// 验证码生成类
13
/// </summary>
14
public static class ValidateCodeHelper
15
/// 生成长度为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
25
const string sNumber = "0123456789";
26
const string sLetter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
27
const string sSymbol = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
28
29
if ((codeType | ValidateCodeType.Chinese) == codeType)
30
return GetChinese(length);
32
}
33
34
StringBuilder blocks = new StringBuilder();
35
StringBuilder builder = new StringBuilder();
36
37
if ((codeType | ValidateCodeType.Number) == codeType)
38
blocks.Append(sNumber);
39
40
if ((codeType | ValidateCodeType.Letter) == codeType)
41
blocks.Append(sLetter);
42
43
if ((codeType | ValidateCodeType.Symbol) == codeType)
44
blocks.Append(sSymbol);
45
46
Random rand = new Random();
47
for (int i = 0; i < length; i++)
48
builder.Append(blocks[rand.Next(blocks.Length)]);
50
}
51
52
return builder.ToString();
53
}
54
55
/// 获取num个中文字符
57
/// </summary>
58
/// <param name="num">字符串长度</param>
59
/// <returns>中文字符</returns>
60
private static string GetChinese(int num)
61
* 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
73
Random rand = new Random();
74
StringBuilder builder = new StringBuilder();
75
for (int i = 0; i < num; i++)
76
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[] builder.Append(code);
82
}
83
return builder.ToString();
84
}
85
86
/// 按照输入的字符串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
Bitmap img = new Bitmap(src.Length * wdpc, height);
96
Graphics g = Graphics.FromImage(img);
97
g.Clear(Color.White);
98
99
Random rand = new Random();
100
for (int i = 0; i < 25; i++)
101
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
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
114
g.Save();
115
for (int i = 0; i < 100; i++)
116
int x = rand.Next(img.Width);
118
int y = rand.Next(img.Height);
119
120
img.SetPixel(x, y, Color.FromArgb(rand.Next()));
121
}
122
123
return img;
124
}
125
}
126
127
/// 验证码类型枚举
130
/// </summary>
131
public enum ValidateCodeType
132
*
135
* Number:
136
* ASCII Code 48 - 57
137
*
138
* Letter:
139
* Uppercase
140
* ASCII Code 65 - 90
141
* Lowercase
142
* ASCII Code 97 - 122
143
*
144
* Symbol:
145
* ASCII Code 33 - 126 except Number and Letter
146
*
147
* Chinese:
148
* Unicode 0xA1A1-0xF7FE
149
*/
150
/// 数字
152
/// </summary>
153
Number = 1,
154
/// 字母
156
/// </summary>
157
Letter = 2,
158
/// 符号
160
/// </summary>
161
Symbol = 4,
162
/// 汉字
164
/// </summary>
165
Chinese = 8
166
}
167
#endregion
168
169
public static partial class Extension
170
public static double ToDouble(this IConvertible o)
173
return Convert.ToDouble(o);
175
}
176
177
public static int ToInt32(this IConvertible o)
178
return Convert.ToInt32(o);
180
}
181
182
public static float ToSingle(this IConvertible o)
183
return Convert.ToSingle(o);
185
}
186
#endregion
187
}
188
}
2
3
4
5
6
7
8
9
10
13
14
15
18
19
20
21
22
23
25
26
27
28
29
30
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
50
51
52
53
54
55
57
58
59
60
61
64
65
66
67
68
69
70
71
72
73
74
75
76
78
79
80
82
83
84
85
86
88
89
90
91
92
93
94
96
97
98
99
100
101
103
104
105
106
107
108
109
110
111
112
113
114
115
116
118
119
120
121
122
123
124
125
126
127
130
131
132
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
152
153
154
156
157
158
160
161
162
164
165
166
167
168
169
170
173
175
176
177
178
180
181
182
183
185
186
187
188