您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關VB.NET中怎么生成驗證碼,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
VB.NET生成驗證碼前臺代碼為:
< 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
VB.NET生成驗證碼生成圖象驗證碼函數
Sub validatecode(ByVal vnum)
Dim img As System.
drawing.BitmapDim g As Graphics
Dim r As Random = New Random
Dim gheight As IntInteger =
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.LinearGradientBrushbrush = 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)
VB.NET生成驗證碼中畫圖片的前景噪音點
'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()
'vnumvnum = vnum & vcarray(Int(35 *
Rnd())) '數組一般從0開始讀取,所以這里為35*rndvnumvnum = vnum & vcarray(Int(29 *
Rnd())) '數組一般從0開始讀取,所以這里為35*rndNext
Return vnum
End Function
以上就是VB.NET中怎么生成驗證碼,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。