用vb.net实现验证码生成程序
首先引入命名空间:
Imports System.Drawing
Imports System.io
Imports System.io
'产生随机码
Private Function generateVCode(ByVal CodeLength As Integer) As String
Dim allChar As String = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,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 allCharArray() As String = allChar.Split(",")
Dim randomCode As String = ""
Dim temp As Integer = -1
Dim rand As Random = New Random
Dim i As Integer = 0
Do While (i < CodeLength)
If (temp <> -1) Then
Dim aa As Integer = CType(DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer) '根据时间生成随机数种子
rand = New Random(aa)
End If
Dim t As Integer = rand.Next(61) + 1
If t > allCharArray.Length - 1 Then t = allCharArray.Length - 1
If temp = t Then '抑制产生连续重复的验证码。
i -= 1
randomCode = Microsoft.VisualBasic.Left(randomCode, i)
End If
temp = t
randomCode = randomCode + allCharArray(t)
i += 1
Loop
Return randomCode
End Function
Private Function generateVCode(ByVal CodeLength As Integer) As String
Dim allChar As String = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,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 allCharArray() As String = allChar.Split(",")
Dim randomCode As String = ""
Dim temp As Integer = -1
Dim rand As Random = New Random
Dim i As Integer = 0
Do While (i < CodeLength)
If (temp <> -1) Then
Dim aa As Integer = CType(DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer) '根据时间生成随机数种子
rand = New Random(aa)
End If
Dim t As Integer = rand.Next(61) + 1
If t > allCharArray.Length - 1 Then t = allCharArray.Length - 1
If temp = t Then '抑制产生连续重复的验证码。
i -= 1
randomCode = Microsoft.VisualBasic.Left(randomCode, i)
End If
temp = t
randomCode = randomCode + allCharArray(t)
i += 1
Loop
Return randomCode
End Function
'创建验证码图象
Private Sub CreateImage(ByVal checkCode As String)
Dim iwidth As Integer = Convert.ToInt32(checkCode.Length * 15)
Dim image As Bitmap = New Bitmap(iwidth, 25)
Dim g As Graphics = Graphics.FromImage(image)
Dim f As Font = New Font("Arial", 12, System.Drawing.FontStyle.Bold) '设置输出的字体的及字大小可作相应调整
Dim b As Brush = New SolidBrush(Color.White)
g.Clear(Color.Green) '填充背景色,可根据页面风格更换相应的颜色
g.DrawString(checkCode, f, b, 3, 3)
Dim rand As Random = New Random
'画图片的前景燥点
For i As Integer = 0 To 60
Dim x As Integer = rand.Next(image.Width)
Dim y As Integer = rand.Next(image.Height)
image.SetPixel(x, y, Color.FromArgb(rand.Next()))
Next
Dim ms As MemoryStream = New System.IO.MemoryStream
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Response.ClearContent()
Response.ContentType = "image/Jpeg"
Response.BinaryWrite(ms.ToArray())
g.Dispose()
image.Dispose()
End Sub
Private Sub CreateImage(ByVal checkCode As String)
Dim iwidth As Integer = Convert.ToInt32(checkCode.Length * 15)
Dim image As Bitmap = New Bitmap(iwidth, 25)
Dim g As Graphics = Graphics.FromImage(image)
Dim f As Font = New Font("Arial", 12, System.Drawing.FontStyle.Bold) '设置输出的字体的及字大小可作相应调整
Dim b As Brush = New SolidBrush(Color.White)
g.Clear(Color.Green) '填充背景色,可根据页面风格更换相应的颜色
g.DrawString(checkCode, f, b, 3, 3)
Dim rand As Random = New Random
'画图片的前景燥点
For i As Integer = 0 To 60
Dim x As Integer = rand.Next(image.Width)
Dim y As Integer = rand.Next(image.Height)
image.SetPixel(x, y, Color.FromArgb(rand.Next()))
Next
Dim ms As MemoryStream = New System.IO.MemoryStream
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Response.ClearContent()
Response.ContentType = "image/Jpeg"
Response.BinaryWrite(ms.ToArray())
g.Dispose()
image.Dispose()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'在此处放置初始化页的用户代码
Dim checkCode As String = generateVCode(4)
Session("CheckCode") = checkCode.ToString
CreateImage(checkCode)
End Sub
现在我把它封装成了web控件, 需要的朋友可以直接拿去用,但还请各位朋友能够帮助改进程序,谢谢!'在此处放置初始化页的用户代码
Dim checkCode As String = generateVCode(4)
Session("CheckCode") = checkCode.ToString
CreateImage(checkCode)
End Sub
验证控件
如果无法下载,请发邮件至wxnet2008@gmail.com