验证码类(转)
这些东西网上都泛滥了,随便拉了个过来,然后改改,加上自己的标示什么的,就是这样子了。
可以定义验证码的格式,包括字母、数字、特殊符号和汉字,指定生成的图片大小。
本来还想加上图片扭曲的,可怕以后用起来被人说“万恶的验证码”所以就算了。
可以定义验证码的格式,包括字母、数字、特殊符号和汉字,指定生成的图片大小。
本来还想加上图片扭曲的,可怕以后用起来被人说“万恶的验证码”所以就算了。
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
{
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
25
const string sNumber = "0123456789";
26
const string sLetter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
27
const string sSymbol = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
28
29
if ((codeType | ValidateCodeType.Chinese) == codeType)
30
{
31
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
{
49
builder.Append(blocks[rand.Next(blocks.Length)]);
50
}
51
52
return builder.ToString();
53
}
54
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
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
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
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
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
{
117
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
ValidateCodeType Enum
168
169
public static partial class Extension
170
{
171
convert method
187
}
188
}
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.Utility10
{11
/// <summary>12
/// 验证码生成类13
/// </summary>14
public static class ValidateCodeHelper15
{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

25
const string sNumber = "0123456789";26
const string sLetter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";27
const string sSymbol = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";28

29
if ((codeType | ValidateCodeType.Chinese) == codeType)30
{31
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
{49
builder.Append(blocks[rand.Next(blocks.Length)]);50
}51

52
return builder.ToString();53
}54

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 use64
* 65
* first byte 0xB0 - 0xD766
* second byte 0xA1 - 0xFE67
* 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 + 172

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

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

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

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
{117
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
ValidateCodeType Enum168

169
public static partial class Extension170
{171
convert method187
}188
}
成功者找方法,失败者找借口!



浙公网安备 33010602011771号