Asp.net(vb.net)驗證碼11/18

来源:互联网 发布:农业科技网络书屋登陆 编辑:程序博客网 时间:2024/05/01 15:16

想實現登錄時加入驗證碼識別,尋找到的大都是C#的參考,而將C#代碼轉為VB.NET時在下面一行紅色的標識中卻找不到對應的寫法,先暫時這樣吧,有好方法再來分享^__^

登錄頁面顯示驗證碼的方法:<asp:Image id="Image1" runat="server" ImageUrl="WebForm31.aspx" Height="24px" Width="80px">

驗證碼頁面(webform31.aspx):Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在這裡放置使用者程式碼以初始化網頁
Dim checkcode As String = CreateRandomCode(4) '設定驗證碼數為4碼
Session("checkcode") = checkcode
CreateCheckCodeImage(checkcode)

End Sub

Private Function CreateRandomCode(ByVal codeCount 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 < codeCount)
If (temp <> -1) Then
rand = New Random(i * temp)
'rand = New Random(i * temp * CType(DateTime.Now.Ticks, Integer)) 這裏有問題尚未找到解決方法,待續
End If
Dim t As Integer = rand.Next(61)
If temp = t Then
Return CreateRandomCode(codeCount)
End If
temp = t
randomCode = randomCode + allCharArray(t)
i += 1
Loop

Return randomCode
End Function

Private Sub CreateCheckCodeImage(ByVal checkCode As String)
If checkCode Is Nothing OrElse checkCode.Trim = String.Empty Then
Return
End If
Dim image As System.Drawing.Bitmap = New System.Drawing.Bitmap(CType(Math.Ceiling((checkCode.Length * 14.5)), Integer), 22)
Dim g As Graphics = Graphics.FromImage(image)
Try
Dim random As Random = New Random
g.Clear(Color.White)
Dim i As Integer = 0
While i < 25
Dim x1 As Integer = random.Next(image.Width)
Dim x2 As Integer = random.Next(image.Width)
Dim y1 As Integer = random.Next(image.Height)
Dim y2 As Integer = random.Next(image.Height)
g.DrawLine(New Pen(Color.Silver), x1, y1, x2, y2)
System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
End While
Dim font As Font = New System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic))
Dim brush As System.Drawing.Drawing2D.LinearGradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, True)
g.DrawString(checkCode, font, brush, 2, 2)
'Dim i As Integer = 0
i = 0
While i < 100
Dim x As Integer = random.Next(image.Width)
Dim y As Integer = random.Next(image.Height)
image.SetPixel(x, y, Color.FromArgb(random.Next))
System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
End While
g.DrawRectangle(New Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1)
Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)
Response.ClearContent()
Response.ContentType = "image/Gif"
Response.BinaryWrite(ms.ToArray)
Finally
g.Dispose()
image.Dispose()
End Try
End Sub

參考來源:http://www.cnblogs.com/index/archive/2004/10/20/54692.aspx; http://dev.csdn.net/article/73/73816.shtm

原创粉丝点击