給學生帶ASP.NET課設,有學生問我網頁上驗證碼怎麼產生?
我到網上搜尋了一下,找到很多網頁,但是代碼全部是一樣的,使用C#編寫的,因為學生只學習了VB.NET,所以我把C#改寫了一下,形成VB.NET代碼如下:
我使用VS.NET2005!
首先在VS.NET2005建立一個網站。
添加一個類,類檔案名稱為:Class1.vb,其內容如下:
Imports Microsoft.VisualBasic
Imports System.Drawing
Public Class CreateImage
Public Shared Sub DrawImage()
Dim img As New CreateImage()
HttpContext.Current.Session("CheckCode") = img.RndNum(4)
img.CreateImages(HttpContext.Current.Session("CheckCode").ToString())
End Sub 'DrawImage
'/ <summary>
'/ 產生驗證圖片
'/ </summary>
'/ <param name="checkCode">驗證字元</param>
Private Sub CreateImages(ByVal checkCode As String)
Dim iwidth As Integer = CInt(checkCode.Length * 13)
Dim image As New System.Drawing.Bitmap(iwidth, 23)
Dim g As Graphics = Graphics.FromImage(image)
g.Clear(Color.White)
'定義顏色
Dim c As Color() = {Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple}
'定義字型
Dim font As String() = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體"}
Dim rand As New Random()
'隨機輸出噪點
Dim i As Integer
For i = 0 To 49
Dim x As Integer = rand.Next(image.Width)
Dim y As Integer = rand.Next(image.Height)
g.DrawRectangle(New Pen(Color.LightGray, 0), x, y, 1, 1)
Next i
'輸出不同字型和顏色的驗證碼字元
For i = 0 To checkCode.Length - 1
Dim cindex As Integer = rand.Next(7)
Dim findex As Integer = rand.Next(5)
Dim f = New System.Drawing.Font(font(findex), 10, System.Drawing.FontStyle.Bold)
Dim b = New System.Drawing.SolidBrush(c(cindex))
Dim ii As Integer = 4
If (i + 1) Mod 2 = 0 Then
ii = 2
End If
g.DrawString(checkCode.Substring(i, 1), f, b, 3 + i * 12, ii)
Next i
'畫一個邊框
g.DrawRectangle(New Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1)
'輸出到瀏覽器
Dim ms As New System.IO.MemoryStream()
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
HttpContext.Current.Response.ClearContent()
'Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Jpeg"
HttpContext.Current.Response.BinaryWrite(ms.ToArray())
g.Dispose()
image.Dispose()
End Sub 'CreateImages
'/ <summary>
'/ 產生隨機的字母
'/ </summary>
'/ <param name="VcodeNum">產生字母的個數</param>
'/ <returns>string</returns>
Private Function RndNum(ByVal VcodeNum 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 < VcodeNum)
If (temp <> -1) Then
Dim key As Integer = CType(DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer)
'用系統時間產生隨機種子
rand = New Random(key)
End If
Dim t As Integer = rand.Next(allCharArray.Length) + 1
If t > allCharArray.Length - 1 Then
t = allCharArray.Length - 1
End If
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 'RndNum
End Class 'CreateImage
然後添加一個網頁,檔案名稱為:Default2.aspx,代碼如下:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>驗證碼產生</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<script language= javascript type="text/javascript" >
<!__
var numkey = Math.random();
numkey = Math.round(numkey*10000);
document.write("<img src=/"Image.aspx?k="+ numkey +"/" width=/"52/" height=/"23/" hspace=/"4/"");
//__>
</script>
</div>
</form>
</body>
</html>
該網頁後代碼檔案名稱為:Default2.aspx.vb,代碼如下:
Imports CreateImage
Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
CreateImage.DrawImage()
End Sub
End Class
運行效果如下: