zoukankan      html  css  js  c++  java
  • VB.net 产生随机验证码

    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Drawing.Drawing2D
    Partial Class _Default
        Inherits System.Web.UI.Page

        Dim path As String


        '產生隨機字串
        Private Function GenCode(ByVal num As Integer) As String
            Dim [source] 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", "V", "W", "X", "Y", "Z"}
            Dim code As String = ""
            Dim rd As Random = New Random()
            Dim i As Integer
            For i = 0 To num - 1 Step i + 1
                code += source(rd.Next(0, source.Length))
            Next
            Return code
        End Function

        '生成圖片
        Private Sub GenImg(ByVal code As String)
            Dim myPalette As Bitmap = New Bitmap(60, 18) '定義一個畫板

            Dim gh As Graphics = Graphics.FromImage(myPalette) '在畫板上定義繪圖的實例

            Dim rc As Rectangle = New Rectangle(0, 0, 60, 18) '定義一個矩形

            gh.FillRectangle(New SolidBrush(Color.DarkSeaGreen), rc) '填充矩形
            gh.DrawString(code, New Font("Arial", 12), New SolidBrush(Color.White), rc) '在矩形內畫出字串

            myPalette.Save(Request.MapPath(path) + "\1.bmp", System.Drawing.Imaging.ImageFormat.Jpeg) '將圖片顯示出來

            Session("Code") = code '將字串保存到Session中,以便需要時進行驗證

            gh.Dispose()
            myPalette.Dispose()
        End Sub

        '生成圖片(增加背景噪音線、前景噪音點)
        Private Sub CreateCheckCodeImage(ByVal checkCode As String)
            If checkCode = Nothing Or checkCode.Trim() = String.Empty Then
                Return
            End If

            Session("Code") = checkCode '將字串保存到Session中,以便需要時進行驗證

            Dim image As System.Drawing.Bitmap = New System.Drawing.Bitmap(CType(Math.Ceiling((checkCode.Length * 12.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
                For i = 0 To 25 - 1 Step i + 1
                    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", 12, (System.Drawing.FontStyle.Bold))
                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)
                Session("checkCode") = checkCode

                '畫圖片的前景噪音點
                Dim j As Integer
                For j = 0 To 100 - 1 Step j + 1
                    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 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

        Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate

            If (Login1.UserName = "1" And Login1.Password = "2") Then
                If Me.TextBox1.Text = Session("Code") Then
                    e.Authenticated = True
                Else
                    e.Authenticated = False
                    Me.Label1.Text = "不成功"
                End If
            End If


        End Sub

        Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
            ' GenImg(GenCode(4))

        End Sub

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

       
        End Sub

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            GenImg(GenCode(4))
            Me.Label1.Text = Session("Code")
        End Sub
    End Class

  • 相关阅读:
    ado.net的基本特性
    The relationship's type
    the relationship's cardinality
    复杂心情中。。。
    the relationship's existence.
    ORACLE中国的短视
    ADO.net连接数据库
    虚拟基类的初始化
    递归实现全排列
    Factory Methods
  • 原文地址:https://www.cnblogs.com/fuyingke/p/766030.html
Copyright © 2011-2022 走看看