1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using System.IO;
12
using System.Drawing;
13
using System.Drawing.Drawing2D;
14
using System.Drawing.Imaging;
15
using System.Drawing.Text;
16
using System.Security.Cryptography;
17
18
public partial class inc_VerifyCode : System.Web.UI.Page
19
{
20
protected void Page_Load(object sender, EventArgs e)
21
{
22
GenerateImage("2312", 155, 40, Color.White, 111111);
23
}
24
25
26
private static byte[] randb = new byte[4];
27
private static RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();
28
29
private static Font[] fonts = {
30
new Font(new FontFamily("Times New Roman"), 20 + Next(4),System.Drawing.FontStyle.Bold),
31
new Font(new FontFamily("Georgia"), 20 + Next(4),System.Drawing.FontStyle.Bold),
32
new Font(new FontFamily("Arial"), 20 + Next(4),System.Drawing.FontStyle.Bold),
33
new Font(new FontFamily("Comic Sans MS"), 20 + Next(4),System.Drawing.FontStyle.Bold)
34
};
35
/// <summary>
36
/// 获得下一个随机数
37
/// </summary>
38
/// <param name="max">最大值</param>
39
/// <returns></returns>
40
private static int Next(int max)
41
{
42
rand.GetBytes(randb);
43
int value = BitConverter.ToInt32(randb, 0);
44
value = value % (max + 1);
45
if (value < 0)
46
value = -value;
47
return value;
48
}
49
50
/// <summary>
51
/// 获得下一个随机数
52
/// </summary>
53
/// <param name="min">最小值</param>
54
/// <param name="max">最大值</param>
55
/// <returns></returns>
56
private static int Next(int min, int max)
57
{
58
int value = Next(max - min) + min;
59
return value;
60
}
61
62
63
public void GenerateImage(string code, int width, int height, Color bgcolor, int textcolor)
64
{
65
66
67
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
68
69
Graphics g = Graphics.FromImage(bitmap);
70
Rectangle rect = new Rectangle(0, 0, width, height);
71
g.SmoothingMode = SmoothingMode.AntiAlias;
72
73
g.Clear(bgcolor);
74
75
//int emSize = Next(3) + 15;//(int)((width - 20) * 2 / text.Length);
76
//FontFamily family = fonts[Next(fonts.Length - 1)];
77
//Font font = new Font(family, emSize, FontStyle.Bold);
78
79
//SizeF measured = new SizeF(0, 0);
80
//SizeF workingSize = new SizeF(width, height);
81
//while (emSize > 2 && (measured = g.MeasureString(code, font)).Width > workingSize.Width || measured.Height > workingSize.Height)
82
//{
83
// font.Dispose();
84
// font = new Font(family, emSize -= 2);
85
//}
86
int fixedNumber = textcolor == 2 ? 60 : 0;
87
88
SolidBrush drawBrush = new SolidBrush(Color.FromArgb(Next(100), Next(100), Next(100)));
89
for (int x = 0; x < 3; x++)
90
{
91
Pen linePen = new Pen(Color.FromArgb(Next(150) + fixedNumber, Next(150) + fixedNumber, Next(150) + fixedNumber), 1);
92
g.DrawLine(linePen, new PointF(0.0F + Next(20), 0.0F + Next(height)), new PointF(0.0F + Next(width), 0.0F + Next(height)));
93
}
94
95
96
Matrix m = new Matrix();
97
for (int x = 0; x < code.Length; x++)
98
{
99
m.Reset();
100
m.RotateAt(Next(30) - 15, new PointF(Convert.ToInt64(width * (0.10 * x)), Convert.ToInt64(height * 0.5)));
101
g.Transform = m;
102
drawBrush.Color = Color.FromArgb(Next(150) + fixedNumber + 20, Next(150) + fixedNumber + 20, Next(150) + fixedNumber + 20);
103
PointF drawPoint = new PointF(0.0F + Next(4) + x * 20, 3.0F + Next(3));
104
g.DrawString(Next(1) == 1 ? code[x].ToString() : code[x].ToString().ToUpper(), fonts[Next(fonts.Length - 1)], drawBrush, drawPoint);
105
g.ResetTransform();
106
}
107
108
109
110
double distort = Next(5, 10) * (Next(10) == 1 ? 1 : -1);
111
112
using (Bitmap copy = (Bitmap)bitmap.Clone())
113
{
114
for (int y = 0; y < height; y++)
115
{
116
for (int x = 0; x < width; x++)
117
{
118
int newX = (int)(x + (distort * Math.Sin(Math.PI * y / 84.5)));
119
int newY = (int)(y + (distort * Math.Cos(Math.PI * x / 54.5)));
120
if (newX < 0 || newX >= width)
121
newX = 0;
122
if (newY < 0 || newY >= height)
123
newY = 0;
124
bitmap.SetPixel(x, y, copy.GetPixel(newX, newY));
125
}
126
}
127
}
128
129
130
//g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1);
131
132
133
134
drawBrush.Dispose();
135
g.Dispose();
136
137
System.IO.MemoryStream ms = new System.IO.MemoryStream();
138
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
139
Response.ClearContent();
140
Response.ContentType = "image/Gif";
141
Response.BinaryWrite(ms.ToArray());
142
}
143
144
}
145

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
