VB.net 产生随机验证码
2007-05-31 10:30
357 查看
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
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
相关文章推荐
- 产生随机验证码(asp.net)
- ASP.NET产生随机验证码
- ASP.NET产生随机验证码
- asp.net(vb.net)产生验证码2
- C#.NET产生在两个值之间的不重复随机数组
- JavaWeb学习记录(五)——Servlet随机产生验证码
- vb.net获取web验证码在winform中显示的简单示例
- vb.net实现验证码功能
- VB.NET生成随机串或随机数字的方法总结
- asp.net(C#) 生成随机验证码的代码
- C#.Net 完美随机验证码
- asp.net(C#) 生成随机验证码的代码
- 在ASP.NET下实现数字和字符相混合的验证码(vb.net Code)
- 随机产生指定位数的验证码
- ASP.NET生成随机验证码代码和看不清切换验证码的小结
- [转] ASP.NET1.1(C#)中验证码产生的原理及应用
- NET 很简单的一个生成随机验证码封装的一个类
- 【wed】wed网页随机产生验证码图片(版本tomcat-8.0.14)
- 用VB产生随机任意大小文件挤满硬盘
- wed网页随机产生验证码图片(版本tomcat-8.0.14)