本篇文章給大家分享的是有關(guān)VB.NET中怎么生成驗(yàn)證碼,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
網(wǎng)站的建設(shè)創(chuàng)新互聯(lián)專注網(wǎng)站定制,經(jīng)驗(yàn)豐富,不做模板,主營(yíng)網(wǎng)站定制開發(fā).小程序定制開發(fā),H5頁(yè)面制作!給你煥然一新的設(shè)計(jì)體驗(yàn)!已為成都工商代辦等企業(yè)提供專業(yè)服務(wù)。
VB.NET生成驗(yàn)證碼前臺(tái)代碼為:
< 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()
在此處放置初始化頁(yè)的用戶代碼,rndnum是一個(gè)自定義函數(shù)
Dim vnum As String
= rndnum(4)Session("vnum") = vnum
validatecode(vnum)
End Function
VB.NET生成驗(yàn)證碼生成圖象驗(yàn)證碼函數(shù)
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為圖片寬度,根據(jù)字符長(zhǎng)度自動(dòng)更改圖片寬度
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生成驗(yàn)證碼中畫圖片的前景噪音點(diǎn)
'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)
在矩形內(nèi)繪制字串(字串,字體,畫筆顏色,左上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
函數(shù)名稱:rndnum
函數(shù)參數(shù):vcodenum--設(shè)定返回隨機(jī)字符串的位數(shù)
函數(shù)功能:產(chǎn)生數(shù)字和字符混合的隨機(jī)字符串
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, ",") '將字符串生成數(shù)組Dim vnum As String = ""
Dim i As Byte
For i = 1 To vcodenum
Randomize()
'vnumvnum = vnum & vcarray(Int(35 *
Rnd())) '數(shù)組一般從0開始讀取,所以這里為35*rndvnumvnum = vnum & vcarray(Int(29 *
Rnd())) '數(shù)組一般從0開始讀取,所以這里為35*rndNext
Return vnum
End Function
以上就是VB.NET中怎么生成驗(yàn)證碼,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。