一个.net图片验证码的实现
页面代码
If Txt_code.Text.Trim <> Session("checkcode").ToString Then
Alert(Page, "输入验证码错误,请重新输入")
Exit Sub
End If主要实现
1
<asp:Image id="Image1" width="100" runat="server" ImageUrl="~/manage/mycheckcode.aspx"></asp:Image>
后台代码实现
<asp:Image id="Image1" width="100" runat="server" ImageUrl="~/manage/mycheckcode.aspx"></asp:Image>
If Txt_code.Text.Trim <> Session("checkcode").ToString Then
Alert(Page, "输入验证码错误,请重新输入")
Exit Sub
End If 1
Imports System.Drawing.Imaging
2
Imports System.Drawing.Drawing2D
3
Public Class MycheckCodedd
4
Inherits System.Web.UI.Page
5![]()
6
Web 窗体设计器生成的代码
24![]()
25
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
26
'在此处放置初始化页的用户代码
27
Response.Buffer = True
28
draw2(getNum.ToString)
29
End Sub
30![]()
31
Private Function getNum() As Int64
32
Dim zNum, i, j As Int64
33![]()
34
Randomize(Timer)
35
zNum = CInt(8999 * Rnd() + 1000)
36
Session("CheckCode") = zNum
37
Return zNum
38
End Function
39![]()
40
Private Sub draw()
41
Response.ContentType = "image/jpeg"
42
Dim width As Int32
43
Dim height As Int32
44
width = 300
45
height = 300
46
Dim x, y As Int16
47
x = 30
48
y = 50
49
Dim zNum, i, j
50
Dim Ados, Ados1
51
Randomize(Timer)
52
zNum = CInt(8999 * Rnd() + 1000)
53
Session("CheckCode") = zNum
54![]()
55
Dim objBitmap As Bitmap
56
objBitmap = New Bitmap(width, height)
57![]()
58
Dim objGraphics As Graphics
59
objGraphics = Graphics.FromImage(objBitmap)
60![]()
61
objGraphics.DrawRectangle(New Pen(Color.Black), 0, 0, width, height)
62
objGraphics.FillRectangle(New SolidBrush(Color.White), 1, 1, width - 2, height - 2)
63![]()
64
Dim objBrush As New SolidBrush(Color.Blue)
65
objGraphics.SmoothingMode = SmoothingMode.AntiAlias
66
objBitmap.Save(Response.OutputStream, ImageFormat.Jpeg)
67
objGraphics.Dispose()
68
objBitmap.Dispose()
69
End Sub
70![]()
71
Private Sub draw2(ByVal str As String)
72
Dim image As New Bitmap(600, 200)
73
Dim axesfont As New Font("arial", 110, FontStyle.Bold)
74![]()
75
Dim blackbrush As New SolidBrush(Color.SteelBlue)
76
Dim redpen As New Pen(Color.Red, 5)
77
Dim blackpen As New Pen(Color.Black, 2)
78
Dim g As Graphics
79![]()
80
g = Graphics.FromImage(image)
81
g.Clear(Color.White)
82
g.FillRectangle(New SolidBrush(Color.White), 0, 0, 600, 200)
83
g.DrawString(str, axesfont, blackbrush, 10, 10)
84
' g.DrawLine(blackpen, 0, 2, 210, 250)
85
' g.DrawLine(redpen, 600, 200, 0, 0)
86
' g.DrawLine(blackpen, 210, 250, 310, 50)
87
' g.DrawLine(redpen, 310, 50, 210, 350)
88
image.Save(Response.OutputStream, ImageFormat.Jpeg)
89![]()
90
End Sub
91![]()
92![]()
93![]()
94
Function NumCode()
95
Response.Expires = -1
96
Response.AddHeader("Pragma", "no-cache")
97
Response.AddHeader("cache-ctrol", "no-cache")
98
On Error Resume Next
99
Dim zNum, i, j
100
Dim Ados, Ados1
101
Randomize(Timer)
102
zNum = CInt(8999 * Rnd() + 1000)
103
Session("CheckCode") = zNum
104
Dim zimg(4), NStr
105
NStr = CStr(zNum)
106
For i = 0 To 3
107
zimg(i) = CInt(Mid(NStr, i + 1, 1))
108
Next
109
Dim Pos
110
Ados = Server.CreateObject("Adodb.Stream")
111
Ados.Mode = 3
112
Ados.Type = 1
113
Ados.Open()
114
Ados1 = Server.CreateObject("Adodb.Stream")
115
Ados1.Mode = 3
116
Ados1.Type = 1
117
Ados1.Open()
118
Ados.LoadFromFile(Server.MapPath("body.Fix"))
119
Ados1.write(Ados.read(1280))
120
For i = 0 To 3
121
Ados.Position = (9 - zimg(i)) * 320
122
Ados1.Position = i * 320
123
Ados1.write(Ados.read(320))
124
Next
125
Ados.LoadFromFile(Server.MapPath("head.fix"))
126![]()
127
Pos = System.Text.Encoding.Default.GetBytes(Ados.read()).Length()
128![]()
129
Ados.Position = Pos
130
For i = 0 To 9 Step 1
131
For j = 0 To 3
132
Ados1.Position = i * 32 + j * 320
133
Ados.Position = Pos + 30 * j + i * 120
134
Ados.write(Ados1.read(30))
135
Next
136
Next
137
Response.ContentType = "image/BMP"
138
Ados.Position = 0
139
Response.BinaryWrite(Ados.read())
140
Ados.Close() : Ados = Nothing
141
Ados1.Close() : Ados1 = Nothing
142
If Err.Description <> "" Then Session("CheckCode") = 9999
143
End Function
144![]()
145![]()
146![]()
147
End Class
148![]()
Imports System.Drawing.Imaging2
Imports System.Drawing.Drawing2D3
Public Class MycheckCodedd4
Inherits System.Web.UI.Page5

6
Web 窗体设计器生成的代码24

25
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load26
'在此处放置初始化页的用户代码27
Response.Buffer = True28
draw2(getNum.ToString)29
End Sub30

31
Private Function getNum() As Int6432
Dim zNum, i, j As Int6433

34
Randomize(Timer)35
zNum = CInt(8999 * Rnd() + 1000)36
Session("CheckCode") = zNum37
Return zNum38
End Function39

40
Private Sub draw()41
Response.ContentType = "image/jpeg"42
Dim width As Int3243
Dim height As Int3244
width = 30045
height = 30046
Dim x, y As Int1647
x = 3048
y = 5049
Dim zNum, i, j50
Dim Ados, Ados151
Randomize(Timer)52
zNum = CInt(8999 * Rnd() + 1000)53
Session("CheckCode") = zNum54

55
Dim objBitmap As Bitmap56
objBitmap = New Bitmap(width, height)57

58
Dim objGraphics As Graphics59
objGraphics = Graphics.FromImage(objBitmap)60

61
objGraphics.DrawRectangle(New Pen(Color.Black), 0, 0, width, height)62
objGraphics.FillRectangle(New SolidBrush(Color.White), 1, 1, width - 2, height - 2)63

64
Dim objBrush As New SolidBrush(Color.Blue)65
objGraphics.SmoothingMode = SmoothingMode.AntiAlias66
objBitmap.Save(Response.OutputStream, ImageFormat.Jpeg)67
objGraphics.Dispose()68
objBitmap.Dispose()69
End Sub70

71
Private Sub draw2(ByVal str As String)72
Dim image As New Bitmap(600, 200)73
Dim axesfont As New Font("arial", 110, FontStyle.Bold)74

75
Dim blackbrush As New SolidBrush(Color.SteelBlue)76
Dim redpen As New Pen(Color.Red, 5)77
Dim blackpen As New Pen(Color.Black, 2)78
Dim g As Graphics79

80
g = Graphics.FromImage(image)81
g.Clear(Color.White)82
g.FillRectangle(New SolidBrush(Color.White), 0, 0, 600, 200)83
g.DrawString(str, axesfont, blackbrush, 10, 10)84
' g.DrawLine(blackpen, 0, 2, 210, 250)85
' g.DrawLine(redpen, 600, 200, 0, 0)86
' g.DrawLine(blackpen, 210, 250, 310, 50)87
' g.DrawLine(redpen, 310, 50, 210, 350)88
image.Save(Response.OutputStream, ImageFormat.Jpeg)89

90
End Sub91

92

93

94
Function NumCode()95
Response.Expires = -196
Response.AddHeader("Pragma", "no-cache")97
Response.AddHeader("cache-ctrol", "no-cache")98
On Error Resume Next99
Dim zNum, i, j100
Dim Ados, Ados1101
Randomize(Timer)102
zNum = CInt(8999 * Rnd() + 1000)103
Session("CheckCode") = zNum104
Dim zimg(4), NStr105
NStr = CStr(zNum)106
For i = 0 To 3107
zimg(i) = CInt(Mid(NStr, i + 1, 1))108
Next109
Dim Pos110
Ados = Server.CreateObject("Adodb.Stream")111
Ados.Mode = 3112
Ados.Type = 1113
Ados.Open()114
Ados1 = Server.CreateObject("Adodb.Stream")115
Ados1.Mode = 3116
Ados1.Type = 1117
Ados1.Open()118
Ados.LoadFromFile(Server.MapPath("body.Fix"))119
Ados1.write(Ados.read(1280))120
For i = 0 To 3121
Ados.Position = (9 - zimg(i)) * 320122
Ados1.Position = i * 320123
Ados1.write(Ados.read(320))124
Next125
Ados.LoadFromFile(Server.MapPath("head.fix"))126

127
Pos = System.Text.Encoding.Default.GetBytes(Ados.read()).Length()128

129
Ados.Position = Pos130
For i = 0 To 9 Step 1131
For j = 0 To 3132
Ados1.Position = i * 32 + j * 320133
Ados.Position = Pos + 30 * j + i * 120134
Ados.write(Ados1.read(30))135
Next136
Next137
Response.ContentType = "image/BMP"138
Ados.Position = 0139
Response.BinaryWrite(Ados.read())140
Ados.Close() : Ados = Nothing141
Ados1.Close() : Ados1 = Nothing142
If Err.Description <> "" Then Session("CheckCode") = 9999143
End Function144

145

146

147
End Class148


浙公网安备 33010602011771号