網路上常常看到發文或登入時需要輸入圖型驗證碼,下面來看看怎麼實做

首先建立一支aspx檔案,命名為:ValidateCode.aspx

HTML頁不用動到,直接切換到後置程式碼中貼上以下代碼

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Drawing
Partial Class ValidateCode
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        ‘ 5位數字的驗證碼
        Dim str_ValidateCode As String = GetRandomNumberString(5)


        ‘ 用於驗證的Session
        Session(”ValidateCode”) = str_ValidateCode
        Me.CreateCheckCodeImage(str_ValidateCode)

    End Sub

 

      ‘ 隨機生成數字字符串
    Public Function GetRandomNumberString(ByVal int_NumberLength As Integer) As String
        Dim str_Number As String = String.Empty
        Dim theRandomNumber As New Random()
        For int_index As Integer = 0 To int_NumberLength - 1
            str_Number += theRandomNumber.[Next](10).ToString()
        Next
        Return str_Number
    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 New System.Drawing.Bitmap(CInt(Math.Ceiling((checkCode.Length * 13.5))), 25)
        Dim g As System.Drawing.Graphics = Graphics.FromImage(image)

        Try


            ‘生成隨機生成器
            Dim random As New Random()

 

            ‘清空圖片背景色
            g.Clear(Color.White)
            For i As Integer = 0 To 24

 

                ‘畫圖片的背景噪音線
                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)
            Next

            Dim font As Font = New System.Drawing.Font(”Arial”, 14, (System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic))

            Dim brush As 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)
            For i As Integer = 0 To 99

 

                ‘畫圖片的前景噪音點
                Dim x As Integer = random.[Next](image.Width)
                Dim y As Integer = random.[Next](image.Height)

                image.SetPixel(x, y, Color.FromArgb(random.[Next]()))
            Next

 

            ‘畫圖片的邊框線
            g.DrawRectangle(New Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1)

            Dim ms As 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
End Class

 

之後在要使用的頁面中當成圖片來使用,使用以下代碼

<asp:TextBox ID=”TextBox” runat=”server”></asp:TextBox>
<asp:Image id=”Image1″ runat=”server” ImageUrl=”ValidateCode.aspx” ImageAlign=”Middle” />

而要實做驗證就在頁面中加入if判斷,例如

Dim vcode As String = Session(”ValidateCode”)
Dim mycode As String = TextBox.Text.Trim

if  mycode = vcode Then
    return true
else
    return false

因為上面程式已經把亂數出來的驗證數值存到Session(”ValidateCode”)內了

所以在這邊只要拿出來用就可以了。

創作者介紹

蛤什麼蛤?

joysdw12 發表在 痞客邦 PIXNET 留言(0) 人氣()