


Private Sub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ValidCode As String
If Request.QueryString("type") Is Nothing Then
ValidCode = RndNum_0(4)
ValidateCode_0(ValidCode)
ElseIf "0".Equals(Request.QueryString("type").ToString) Then
ValidCode = RndNum_0(4)
ValidateCode_0(ValidCode)
ElseIf "1".Equals(Request.QueryString("type").ToString) Then
ValidCode = RndNum_1(4)
ValidateCode_1(ValidCode)
ElseIf "2".Equals(Request.QueryString("type").ToString) Then
ValidCode = RndNum_2(4)
ValidateCode_2(ValidCode)
ElseIf "3".Equals(Request.QueryString("type").ToString) Then
ValidCode = RndNum_3(4)
ValidateCode_3(ValidCode)
ElseIf "4".Equals(Request.QueryString("type").ToString) Then

ElseIf "5".Equals(Request.QueryString("type").ToString) Then

Else
ValidCode = RndNum_0(4)
ValidateCode_0(ValidCode)
End If
End Sub


ImageType 0#Region "ImageType 0"

Function RndNum_0()Function RndNum_0(ByVal VcodeNum As Integer) As String
Dim Vchar 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,W,X,Y,Z"
Dim VcArray() As String = Split(Vchar, ",")
Dim VNum As String = ""
Dim i As Byte
For i = 1 To VcodeNum
Randomize()
VNum = VNum & VcArray(Int(35 * Rnd()))
Next
Return VNum
End Function


Sub ValidateCode_0()Sub ValidateCode_0(ByVal ValidCode As String)
Dim Img As System.Drawing.Bitmap
Dim g As Graphics
Dim ms As MemoryStream
Dim gheight As Integer = Int(Len(ValidCode) * 11.5)

Img = New Bitmap(gheight, 20)
g = Graphics.FromImage(Img)
g.Clear(Color.White)
g.DrawString(ValidCode, (New Font("Arial", 10)), (New SolidBrush(Color.Blue)), 3, 3)
g.DrawRectangle(New Pen(Color.Silver), 0, 0, Img.Width - 1, Img.Height - 1)
ms = New MemoryStream
Img.Save(ms, ImageFormat.Png)
Response.ClearContent()
Response.ContentType = "image/Png"
Response.BinaryWrite(ms.ToArray())
g.Dispose()
Img.Dispose()
Response.End()
End Sub
#End Region


ImageType 1#Region "ImageType 1"


Function RndNum_1()Function RndNum_1(ByVal VcodeNum As Integer) As String
Dim intI As Integer
Dim strCode As String = ""
Dim RndNum As New Random
For intI = 1 To VcodeNum
strCode &= RndNum.Next(10).ToString
Next
Return strCode
End Function


Function GetRandowColor()Function GetRandowColor() As Color
Dim RndNum_Frist As New Random(Now.Ticks.GetHashCode)
System.Threading.Thread.Sleep(RndNum_Frist.Next(50))
Dim RndNum_Sencond As New Random(Now.Ticks.GetHashCode)
Dim int_Red As Integer = RndNum_Frist.Next(256)
Dim int_Green As Integer = RndNum_Sencond.Next(256)
Dim int_Blue As Integer = IIf(int_Red + int_Green > 400, 0, 400 - int_Red - int_Green)
int_Blue = IIf(int_Blue > 255, 255, int_Blue)
Return Color.FromArgb(int_Red, int_Green, int_Blue)
End Function


Sub ValidateCode_1()Sub ValidateCode_1(ByVal ValidCode As String)
Dim intI As Integer
Dim str_Char As String
Dim int_ImageWidth As Integer = ValidCode.Length * 13
Dim newRandom As New Random
Dim theBitmap As New Bitmap(int_ImageWidth, 20)
Dim theGraphics As Graphics = Graphics.FromImage(theBitmap)
Dim newBrush As Brush
Dim thePos As PointF
Dim ms As MemoryStream

theGraphics.Clear(Color.White)
theGraphics.DrawRectangle(New Pen(Color.LightGray, 1), 0, 0, int_ImageWidth - 1, 19)
Dim theFont As Font = New Font("Arial", 10)
For intI = 0 To ValidCode.Length - 1
str_Char = ValidCode.Substring(intI, 1)
newBrush = New SolidBrush(GetRandowColor())
thePos = New PointF(intI * 13 + 1 + newRandom.Next(3), 1 + newRandom.Next(3))
theGraphics.DrawString(str_Char, theFont, newBrush, thePos)
Next

ms = New MemoryStream
theBitmap.Save(ms, ImageFormat.Png)
Response.ClearContent()
Response.ContentType = "image/Png"
Response.BinaryWrite(ms.ToArray())
theGraphics.Dispose()
theBitmap.Dispose()
Response.End()
End Sub


#End Region


ImageType 2#Region "ImageType 2"

Function RndNum_2()Function RndNum_2(ByVal CodeLen As Integer) As String
Dim intI As Integer
Dim strCode As String = ""
Dim RndNum As New Random
For intI = 1 To CodeLen
strCode &= RndNum.Next(10).ToString
Next
Return strCode
End Function


Sub ValidateCode_2()Sub ValidateCode_2(ByVal ValidCode As String)
Dim intI As Integer
Dim str_Char As String
Dim int_ImageWidth As Integer = ValidCode.Length * 12
Dim newRandom As New Random
Dim Image As New Bitmap(int_ImageWidth, 20)
Dim g As Graphics = Graphics.FromImage(Image)
Dim brush As Drawing2D.LinearGradientBrush
Dim ms As MemoryStream
Dim x1, x2, y1, y2, x, y As Integer

g.Clear(Color.White)
Dim theFont As Font = New Font("Arial", 10)
brush = New Drawing2D.LinearGradientBrush(New Rectangle(0, 0, Image.Width, Image.Height), Color.Blue, Color.DarkRed, 1.2F, True)
g.DrawString(ValidCode, theFont, brush, 4, 4)
For intI = 0 To 30
x1 = newRandom.Next(Image.Width)
x2 = newRandom.Next(Image.Width)
y1 = newRandom.Next(Image.Height)
y2 = newRandom.Next(Image.Height)
Next
For intI = 0 To 80
x = newRandom.Next(Image.Width)
y = newRandom.Next(Image.Height)
Image.SetPixel(x, y, Color.FromArgb(newRandom.Next()))
Next
g.DrawRectangle(New Pen(Color.Silver), 0, 0, Image.Width - 1, Image.Height - 1)
ms = New MemoryStream
Image.Save(ms, ImageFormat.Png)
Response.ClearContent()
Response.ContentType = "image/Png"
Response.BinaryWrite(ms.ToArray())
g.Dispose()
Image.Dispose()
Response.End()
End Sub
#End Region


ImageType 3#Region "ImageType 3"

Function RndNum_3()Function RndNum_3(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


Sub ValidateCode_3()Sub ValidateCode_3(ByVal validatenum As String)
Dim image As Bitmap = New Bitmap(CInt(Math.Ceiling(validatenum.Length * 12.5)), 20)
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
brush = 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)
Response.Clear()
Response.ContentType = "image/jpeg"
Response.BinaryWrite(stream.ToArray())
Catch ex As Exception

Finally
g.Dispose()
image.Dispose()

End Try
End Sub

#End Region

posted @ 2007-04-05 17:42
BoyLee 阅读(377)
评论(2) 编辑 收藏 所属分类:
Asp.Net