下面的代码我已经测试成功。
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Drawing;
11
using System.Drawing.Imaging;
12
13
14
15
public partial class _Default : System.Web.UI.Page
16
{
17
//该页面将用于生成验证码图片
18
19
protected void Page_Load(object sender, EventArgs e)
20
{
21
//调用函数将验证码生成图片
22
this.CreateCheckCodeImage(GenerateCheckCode());
23
24
}
25
26
private string GenerateCheckCode()
27
{ //产生五位的随机字符串
28
int number;
29
char code;
30
string checkCode = String.Empty;
31
32
System.Random random = new Random();
33
34
for (int i = 0; i < 5; i++)
35
{
36
number = random.Next();
37
38
if (number % 2 == 0)
39
code = (char)('0' + (char)(number % 10));
40
else
41
code = (char)('A' + (char)(number % 26));
42
43
checkCode += code.ToString();
44
}
45
46
//Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
47
Session["CheckCode"] = checkCode;//用于客户端校验码比较
48
49
return checkCode;
50
}
51
52
private void CreateCheckCodeImage(string checkCode)
53
{ //将验证码生成图片显示
54
if (checkCode == null || checkCode.Trim() == String.Empty)
55
return;
56
57
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
58
Graphics g = Graphics.FromImage(image);
59
60
try
61
{
62
//生成随机生成器
63
Random random = new Random();
64
65
//清空图片背景色
66
g.Clear(Color.White);
67
68
//画图片的背景噪音线
69
for (int i = 0; i < 25; i++)
70
{
71
int x1 = random.Next(image.Width);
72
int x2 = random.Next(image.Width);
73
int y1 = random.Next(image.Height);
74
int y2 = random.Next(image.Height);
75
76
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
77
}
78
79
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
80
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
81
g.DrawString(checkCode, font, brush, 2, 2);
82
83
//画图片的前景噪音点
84
for (int i = 0; i < 100; i++)
85
{
86
int x = random.Next(image.Width);
87
int y = random.Next(image.Height);
88
89
image.SetPixel(x, y, Color.FromArgb(random.Next()));
90
}
91
92
//画图片的边框线
93
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
94
95
System.IO.MemoryStream ms = new System.IO.MemoryStream();
96
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
97
Response.ClearContent();
98
Response.ContentType = "image/Gif";
99
Response.BinaryWrite(ms.ToArray());
100
}
101
finally
102
{
103
g.Dispose();
104
image.Dispose();
105
}
106
}
107
108
109
}
下面是测试验证码的页面:
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

1
<div>
2
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
3
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
4
5
<asp:Image ID="Image1" runat="server" ImageUrl="Default.aspx" />//该图片将用于显示验证码
6
7
</div>

2

3

4

5

6

7

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
12
public partial class Test : System.Web.UI.Page
13
{
14
protected void Page_Load(object sender, EventArgs e)
15
{
16
17
}
18
protected void Button1_Click(object sender, EventArgs e)
19
{
20
if (this.TextBox1.Text.ToString().Trim() == Session["CheckCode"].ToString())
21
{
22
Response.Write("<script lauguage='javascript'>alert('验证成功');</script>");
23
}
24
else
25
{
26
Response.Write("<script lauguage='javascript'>alert('验证码错误,请重新输入!');</script>");
27
}
28
29
}
30
}

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
