前台代码为:
<asp:image id="Image2" runat="server" ImageUrl="ValidateCode.aspx">
ValidateCode.aspx文件的代码为:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
list()
End Sub
Function list()
'在此处放置初始化页的用户代码
'rndnum是一个自定义函数
Dim vnum As String = rndnum(4)
Session("vnum") = vnum
validatecode(vnum)
End Function
'生成图象验证码函数
Sub validatecode(ByVal vnum)
Dim img As System.drawing.Bitmap
Dim g As Graphics
Dim r As Random = New Random
Dim gheight As Integer = Int(Len(vnum) * 13)
''gheight为图片宽度,根据字符长度自动更改图片宽度
img = New Bitmap(gheight, 20)
g = Graphics.FromImage(img)
'g.DrawString(vnum, New System.Drawing.Font("Arial", 10), New System.Drawing.SolidBrush(Color.Blue), 3, 3)
'新增,修改
'画图片的背景噪音线
'For i As Integer = 0 To 25
For i As Integer = 0 To 10
Dim x1 As Integer
x1 = r.Next(img.Width)
Dim x2 As Integer = r.Next(img.Width)
Dim y1 As Integer = r.Next(img.Height)
Dim y2 As Integer = r.Next(img.Height)
g.DrawLine(New Pen(Color.Silver), x1, y1, x2, y2)
Next i
Dim font As font
font = New System.Drawing.Font("Arial", 12)
Dim brush As System.Drawing.Drawing2D.LinearGradientBrush
brush = New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, True)
g.DrawString(vnum, font, brush, 2, 2)
''画图片的前景噪音点
'For ii As Integer = 0 To 100
' Dim x As Integer = r.Next(img.Width)
' Dim y As Integer = r.Next(img.Height)
' img.SetPixel(x, y, Color.FromArgb(r.Next()))
'Next
'画图片的边框线
g.DrawRectangle(New Pen(Color.Silver), 0, 0, img.Width - 1, img.Height - 1)
'在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
Dim ms1 As System.IO.MemoryStream
ms1 = New System.IO.MemoryStream
img.Save(ms1, System.Drawing.Imaging.ImageFormat.Png)
Response.ClearContent() '需要输出图象信息 要修改HTTP头
Response.ContentType = "image/Png"
Response.BinaryWrite(ms1.ToArray())
g.Dispose()
img.Dispose()
Response.End()
End Sub
'--------------------------------------------
'函数名称:rndnum
'函数参数:vcodenum--设定返回随机字符串的位数
'函数功能:产生数字和字符混合的随机字符串
'--------------------------------------------
Function rndnum(ByVal vcodenum)
'Dim vchar As String = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z"
Dim vchar As String = "2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,N,P,Q,R,S,T,U,X,Y,Z"
Dim vcarray() As String = Split(vchar, ",") '将字符串生成数组
Dim vnum As String = ""
Dim i As Byte
For i = 1 To vcodenum
Randomize()
'vnum = vnum & vcarray(Int(35 * Rnd())) '数组一般从0开始读取,所以这里为35*rnd
vnum = vnum & vcarray(Int(29 * Rnd())) '数组一般从0开始读取,所以这里为35*rnd
Next
Return vnum
End Function
<asp:image id="Image2" runat="server" ImageUrl="ValidateCode.aspx">
ValidateCode.aspx文件的代码为:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
list()
End Sub
Function list()
'在此处放置初始化页的用户代码
'rndnum是一个自定义函数
Dim vnum As String = rndnum(4)
Session("vnum") = vnum
validatecode(vnum)
End Function
'生成图象验证码函数
Sub validatecode(ByVal vnum)
Dim img As System.drawing.Bitmap
Dim g As Graphics
Dim r As Random = New Random
Dim gheight As Integer = Int(Len(vnum) * 13)
''gheight为图片宽度,根据字符长度自动更改图片宽度
img = New Bitmap(gheight, 20)
g = Graphics.FromImage(img)
'g.DrawString(vnum, New System.Drawing.Font("Arial", 10), New System.Drawing.SolidBrush(Color.Blue), 3, 3)
'新增,修改
'画图片的背景噪音线
'For i As Integer = 0 To 25
For i As Integer = 0 To 10
Dim x1 As Integer
x1 = r.Next(img.Width)
Dim x2 As Integer = r.Next(img.Width)
Dim y1 As Integer = r.Next(img.Height)
Dim y2 As Integer = r.Next(img.Height)
g.DrawLine(New Pen(Color.Silver), x1, y1, x2, y2)
Next i
Dim font As font
font = New System.Drawing.Font("Arial", 12)
Dim brush As System.Drawing.Drawing2D.LinearGradientBrush
brush = New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, True)
g.DrawString(vnum, font, brush, 2, 2)
''画图片的前景噪音点
'For ii As Integer = 0 To 100
' Dim x As Integer = r.Next(img.Width)
' Dim y As Integer = r.Next(img.Height)
' img.SetPixel(x, y, Color.FromArgb(r.Next()))
'Next
'画图片的边框线
g.DrawRectangle(New Pen(Color.Silver), 0, 0, img.Width - 1, img.Height - 1)
'在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
Dim ms1 As System.IO.MemoryStream
ms1 = New System.IO.MemoryStream
img.Save(ms1, System.Drawing.Imaging.ImageFormat.Png)
Response.ClearContent() '需要输出图象信息 要修改HTTP头
Response.ContentType = "image/Png"
Response.BinaryWrite(ms1.ToArray())
g.Dispose()
img.Dispose()
Response.End()
End Sub
'--------------------------------------------
'函数名称:rndnum
'函数参数:vcodenum--设定返回随机字符串的位数
'函数功能:产生数字和字符混合的随机字符串
'--------------------------------------------
Function rndnum(ByVal vcodenum)
'Dim vchar As String = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z"
Dim vchar As String = "2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,N,P,Q,R,S,T,U,X,Y,Z"
Dim vcarray() As String = Split(vchar, ",") '将字符串生成数组
Dim vnum As String = ""
Dim i As Byte
For i = 1 To vcodenum
Randomize()
'vnum = vnum & vcarray(Int(35 * Rnd())) '数组一般从0开始读取,所以这里为35*rnd
vnum = vnum & vcarray(Int(29 * Rnd())) '数组一般从0开始读取,所以这里为35*rnd
Next
Return vnum
End Function