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 ValiImg
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' 6位數的驗證碼
Dim str_ValidateCode As String = GetRandomNumberString(6)
' 用於驗證的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()
Dim rand As New Random
Dim strArr As New ArrayList
For i As Integer = 1 To 26
If Chr(i + 64) = "I" Or Chr(i + 64) = "O" Then
strArr.Add(Chr(65)) '避免英文 I O 造成誤判為 數字 1 0
Else
strArr.Add(Chr(i + 64))
End If
Next
str_Number += strArr(rand.Next(26)).ToString
For int_index As Integer = 1 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
沒有留言:
張貼留言