-
Notifications
You must be signed in to change notification settings - Fork 0
/
VerificationCodeCreator.cs
198 lines (188 loc) · 7.86 KB
/
VerificationCodeCreator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace Limlzy.VerificationCode
{
//=========================================================================
// * 作者: 杨泉耀
// * 时间: 2017/3/1 11:57:26
// * 文件名: VerificationCodeCreator
// * 版本: 1.0
// * 说明:
//=========================================================================
public class VerificationCodeCreator
{
public Confusionlevel Level { get; set; }
public CharSet Charset { get; set; }
public bool AutoSize { get; set; }
public SizeF ImageSize { get; set; }
public string Content { get; set; }
public string CodeString { get; private set; }
public Bitmap Image { get; private set; }
public float FontSize { get; set; }
public Color BackColor { get; set; }
private static Color[] _Colors = new Color[] {
Color.FromArgb(21,163,133),
Color.FromArgb(200,29,43,83),
Color.FromArgb(252,101,101),
Color.FromArgb(153,202,186),
Color.FromArgb(243,108,104),
Color.FromArgb(33,44,55),
Color.FromArgb(21,165,133)
};
public string FontFamily { get;set; }
public static Color[] Colors { get { return _Colors; } }
public VerificationCodeCreator()
{
AutoSize = true;
FontSize = 20;
FontFamily="Microsoft YaHei";
BackColor = Color.Transparent;
}
public int Length
{
get
{
if (CodeString == null)
return 0;
else
return CodeString.Length;
}
}
public void CreateImage(int len=4,string chars = null)
{
if (chars == null)
{
CodeString = CreateRandString(Charset, len);
}
else
{
CodeString = chars;
}
if (AutoSize)
{
ImageSize = new SizeF((float)FontSize*len+len*10,FontSize*(float)1.4);
}
Image = new Bitmap((int)ImageSize.Width,(int)ImageSize.Height);
Graphics g = Graphics.FromImage(Image);
if (BackColor != Color.Transparent)
{
g.Clear(BackColor);
}
//g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
Random rand=new Random((int )DateTime.Now.Ticks);
for (int i=0;i<CodeString.Length;i++)
{
Pen p=new Pen(Colors[rand.Next(0,Colors.Length)]);
g.DrawString(CodeString[i].ToString(), new Font(new FontFamily(FontFamily), (float)FontSize, FontStyle.Regular, GraphicsUnit.Pixel), p.Brush, new PointF(i*FontSize+(i+1)*5,5));
for (int k = 0; k < (int)Level; k++)
{
g.DrawLine(p, new Point(rand.Next(0, (int)ImageSize.Width), rand.Next(0, (int)ImageSize.Height)), new Point(rand.Next(0, (int)ImageSize.Width), rand.Next(0, (int)ImageSize.Height)));
}
}
g.Dispose();
}
public static string CreateRandString(CharSet set, int length)
{
if (length == 0)
throw new ArgumentException("错误的参数值,长度不能为0");
StringBuilder sb = new StringBuilder(length);
int chinaStart = 0x4e00;
int chinaEnd = 0x9fa5;
int numberStart = 48;
int numberEnd = numberStart + 10;
int englishStart = 65;
int englishEnd = 65 + 26;
Random rand = new Random((int)DateTime.Now.Ticks);
for (int i = 0; i < length; i++)
{
switch (set)
{
case CharSet.China:
sb.Append((Char)rand.Next(chinaStart, chinaEnd));
break;
case CharSet.Number:
sb.Append((Char)rand.Next(numberStart, numberEnd));
break;
case CharSet.English:
var char_ = (Char)rand.Next(englishStart, englishEnd);
if (rand.Next(1, 10) % 2 == 0)
char_ += ' ';
sb.Append(char_);
break;
default:
if (set == (CharSet.China | CharSet.English))
{
if (rand.Next(1, 10) % 2 == 0)
{
sb.Append((Char)rand.Next(chinaStart, chinaEnd));
}
else
{
var charLower = (Char)rand.Next(englishStart, englishEnd);
if (rand.Next(1, 10) % 2 == 0)
charLower += ' ';
sb.Append(charLower);
}
}
else if (set == (CharSet.China | CharSet.Number))
{
if (rand.Next(1, 10) % 2 == 0)
{
sb.Append((Char)rand.Next(chinaStart, chinaEnd));
}
else
{
sb.Append((Char)rand.Next(numberStart, numberEnd));
}
}
else if (set == (CharSet.Number | CharSet.English))
{
if (rand.Next(1, 10) % 2 == 0)
{
var charLower = (Char)rand.Next(englishStart, englishEnd);
if (rand.Next(1, 10) % 2 == 0)
charLower += ' ';
sb.Append(charLower);
}
else
{
sb.Append((Char)rand.Next(numberStart, numberEnd));
}
}
else if (set == (CharSet.Number | CharSet.English | CharSet.China))
{
var arg = rand.Next(1, 15) % 3;
if (arg == 0)
{
sb.Append((Char)rand.Next(chinaStart, chinaEnd));
}
else if (arg == 1)
{
var charLower = (Char)rand.Next(englishStart, englishEnd);
if (rand.Next(1, 10) % 2 == 0)
charLower += ' ';
sb.Append(charLower);
}
else if (arg == 2)
{
sb.Append((Char)rand.Next(chinaStart, chinaEnd));
}
}
else
{
throw new ArgumentException("无效的Charset组合");
}
break;
}
}
return sb.ToString();
}
}
}