还记得以前我的BLOGS上被别人发了很多垃圾广告留言,每一个小时被发上一条,因为那时这个BLOG发留言可以直接发,别人是用机器自动发的,很是头痛,那时就叫DUDU给发留言加上验证码.DUDU动作很快,几下就搞上了,别人的机器自动发就不行了,现在就舒服多了.
今天无意中看到别人写的一个自动生成验证码的程序,不过是C#的.还好看了一些时间的C#,能看懂了,为了方便,还是改写成了vb.net的类,这样存在这里,也方便以后自己好查用.
这里是生成的效果:
下面是类的代码,有一些简单的注释,能看明白了.嘿嘿!
Imports
System
Imports System.Drawing.Drawing2D
Imports System.Drawing.imaging
Imports System.Drawing
Imports System.Web.UI
Imports System.IO
Namespace Validate
Public Class CreatValidateNumber
'生成验证码
Public Function CreatValidateNumber(ByVal length As Integer) As String
Dim randNumber() As Integer = New Integer(length) {}
Dim ValidateNumber() As Integer = New Integer(length) {}
Dim ValidateNumberStr As String = ""
Dim seekseek As Integer = CInt(DateTime.Now.Second)
Dim seekrand As Random = New Random(seekseek)
Dim beginseek = Int(seekrand.Next(0, Int32.MaxValue - length * 10000))
Dim seeks() As Integer = New Integer(length) {}
Dim i As Integer
'生成起始序列值
For i = 0 To length - 1
beginseek += 10000
seeks(i) = beginseek
Next
'生成随机数字
For i = 0 To length - 1
Dim rand As Random = New Random(seeks(i))
Dim pownum As Integer = 1 * Int(Math.Pow(10, length))
randNumber(i) = rand.Next(pownum, Int32.MaxValue)
Next
'抽取随机数字
For i = 0 To length - 1
Dim numstr As String = randNumber(i).ToString
Dim numlength As Integer = numstr.Length
Dim rand As New Random
Dim numPosition As Integer = rand.Next(0, numlength - 1)
ValidateNumber(i) = Int32.Parse(numstr.Substring(numPosition, 1))
Next
'生成验证码
For i = 0 To length - 1
ValidateNumberStr += ValidateNumber(i).ToString
Next
Return ValidateNumberStr
End Function
'创建验证码图片
Public Sub CreatevalidateGraphic(ByVal page As Page, ByVal validatenum As String)
Dim image As Bitmap = New Bitmap(CInt(Math.Ceiling(validatenum.Length * 12.5)), 22)
Dim g As Graphics = Graphics.FromImage(image)
Try
'生成随机生成器
Dim random As New Random
'清空图片背景色
g.Clear(Color.White)
'画图片的干扰线
Dim i As Integer
For i = 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 Font("Arial", 12, (FontStyle.Bold And FontStyle.Italic))
Dim brush As LinearGradientBrush = New LinearGradientBrush(New Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, True)
g.DrawString(validatenum, font, brush, 3, 2)
'画前景干扰点
For i = 0 To 24
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 stream As New MemoryStream
image.Save(stream, ImageFormat.Jpeg)
'输出图片
page.Response.Clear()
page.Response.ContentType = "image/jpeg"
page.Response.BinaryWrite(stream.ToArray())
Catch ex As Exception
Finally
g.Dispose()
image.Dispose()
End Try
End Sub
End Class
End Namespace
Imports System.Drawing.Drawing2D
Imports System.Drawing.imaging
Imports System.Drawing
Imports System.Web.UI
Imports System.IO
Namespace Validate
Public Class CreatValidateNumber
'生成验证码
Public Function CreatValidateNumber(ByVal length As Integer) As String
Dim randNumber() As Integer = New Integer(length) {}
Dim ValidateNumber() As Integer = New Integer(length) {}
Dim ValidateNumberStr As String = ""
Dim seekseek As Integer = CInt(DateTime.Now.Second)
Dim seekrand As Random = New Random(seekseek)
Dim beginseek = Int(seekrand.Next(0, Int32.MaxValue - length * 10000))
Dim seeks() As Integer = New Integer(length) {}
Dim i As Integer
'生成起始序列值
For i = 0 To length - 1
beginseek += 10000
seeks(i) = beginseek
Next
'生成随机数字
For i = 0 To length - 1
Dim rand As Random = New Random(seeks(i))
Dim pownum As Integer = 1 * Int(Math.Pow(10, length))
randNumber(i) = rand.Next(pownum, Int32.MaxValue)
Next
'抽取随机数字
For i = 0 To length - 1
Dim numstr As String = randNumber(i).ToString
Dim numlength As Integer = numstr.Length
Dim rand As New Random
Dim numPosition As Integer = rand.Next(0, numlength - 1)
ValidateNumber(i) = Int32.Parse(numstr.Substring(numPosition, 1))
Next
'生成验证码
For i = 0 To length - 1
ValidateNumberStr += ValidateNumber(i).ToString
Next
Return ValidateNumberStr
End Function
'创建验证码图片
Public Sub CreatevalidateGraphic(ByVal page As Page, ByVal validatenum As String)
Dim image As Bitmap = New Bitmap(CInt(Math.Ceiling(validatenum.Length * 12.5)), 22)
Dim g As Graphics = Graphics.FromImage(image)
Try
'生成随机生成器
Dim random As New Random
'清空图片背景色
g.Clear(Color.White)
'画图片的干扰线
Dim i As Integer
For i = 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 Font("Arial", 12, (FontStyle.Bold And FontStyle.Italic))
Dim brush As LinearGradientBrush = New LinearGradientBrush(New Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, True)
g.DrawString(validatenum, font, brush, 3, 2)
'画前景干扰点
For i = 0 To 24
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 stream As New MemoryStream
image.Save(stream, ImageFormat.Jpeg)
'输出图片
page.Response.Clear()
page.Response.ContentType = "image/jpeg"
page.Response.BinaryWrite(stream.ToArray())
Catch ex As Exception
Finally
g.Dispose()
image.Dispose()
End Try
End Sub
End Class
End Namespace
HTML部份:
@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="ValidateNumber.WebForm1"%>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体"> validates.CreatevalidateGraphic(Me, vali) %>FONT>
form>
body>
HTML>
程序部份:
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体"> validates.CreatevalidateGraphic(Me, vali) %>FONT>
form>
body>
HTML>
Public
Class WebForm1
Inherits System.Web.UI.Page
Web 窗体设计器生成的代码
Public validates As New Validate.CreatValidateNumber
Public vali As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
vali = validates.CreatValidateNumber(5)
'validate.CreatevalidateGraphic(Me, vali)
End Sub
End Class
Inherits System.Web.UI.Page
Web 窗体设计器生成的代码
Public validates As New Validate.CreatValidateNumber
Public vali As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
vali = validates.CreatValidateNumber(5)
'validate.CreatevalidateGraphic(Me, vali)
End Sub
End Class
存于此,以供它日之用,还请大家指教.