现在的位置: 首页 > 综合 > 正文

从cnblos.com分离出来的验证码。

2012年05月21日 ⁄ 综合 ⁄ 共 7079字 ⁄ 字号 评论关闭
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Drawing.Text

Public Class loginImg
    
Public Class loginImg
        
Public Sub New()
        
End Sub
 'New
        ' TODO: 在此处添加构造函数逻辑

        
Private [text] As String
        
Private width As Integer
        
Private height As Integer
        
Public image As Bitmap
        
Private random As New random
        
Private familyName As String


        
'Public Sub New(ByVal s As String, ByVal width As Integer, ByVal height As Integer)
        '    Me.Text = s
        '    Me.SetDimensions(width, height)
        '    Me.GenerateImage()
        'End Sub 'New


        
' ====================================================================
        ' Initializes a new instance of the CaptchaImage class using the
        ' specified text, width, height and font family.
        ' ====================================================================
        Public Sub addimg(ByVal s As StringByVal width As IntegerByVal height As IntegerByVal familyName As String)
            
Me.text = s
            
Me.SetDimensions(width, height)
            
Me.SetFamilyName(familyName)
            
Me.GenerateImage()
        
End Sub
 'New


        
Private Sub GenerateImage()
            
' Create a new 32-bit bitmap image.
            Dim bitmap As New Bitmap(Me.width, Me.height, PixelFormat.Format32bppArgb)

            
' Create a graphics object for drawing.
            Dim g As Graphics = Graphics.FromImage(bitmap)
            g.SmoothingMode 
= SmoothingMode.AntiAlias
            
Dim rect As New RectangleF(00Me.width, Me.height)

            
' Fill in the background.
            Dim hatchBrush As New HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White)
            g.FillRectangle(hatchBrush, rect)

            
' Set up the text font.
            Dim size As SizeF
            
Dim fontSize As Single = rect.Height + 1
            
Dim font As Font
            
' Adjust the font size until the text fits within the image.
            Do
                fontSize 
-= 1
                font 
= New Font(Me.familyName, fontSize, FontStyle.Bold)
                size 
= g.MeasureString(Me.text, font)
            
Loop While size.Width > rect.Width

            
' Set up the text format.
            Dim format As New StringFormat
            
format.Alignment = StringAlignment.Center
            
format.LineAlignment = StringAlignment.Center

            
' Create a path using the text and warp it randomly.
            Dim path As New GraphicsPath
            path.AddString(
Me.text, font.FontFamily, CInt(font.Style), font.Size, rect, format)
            
Dim v As Single = 4.0F
            
Dim points As PointF() = {New PointF(Me.random.Next(rect.Width) / v, Me.random.Next(rect.Height) / v), New PointF(rect.Width - Me.random.Next(rect.Width) / v, Me.random.Next(rect.Height) / v), New PointF(Me.random.Next(rect.Width) / v, rect.Height - Me.random.Next(rect.Height) / v), New PointF(rect.Width - Me.random.Next(rect.Width) / v, rect.Height - Me.random.Next(rect.Height) / v)}
            
Dim matrix As New Matrix
            matrix.Translate(
0.0F, 0.0F)

            path.Warp(points, rect, matrix, WarpMode.Perspective, 
0.0F)

            
' Draw the text.
            hatchBrush = New HatchBrush(HatchStyle.LargeConfetti, Color.DarkGray, Color.DarkGray)
            g.FillPath(hatchBrush, path)

            
' Add some random noise.
            Dim m As Integer = Math.Max(rect.Width, rect.Height)
            
Dim i As Integer
            
For i = 0 To CInt(rect.Width * rect.Height / 30.0F) - 1
                
Dim x As Integer = Me.random.Next(rect.Width)
                
Dim y As Integer = Me.random.Next(rect.Height)
                
Dim w As Integer = Me.random.Next((m / 50))
                
Dim h As Integer = Me.random.Next((m / 50))
                g.FillEllipse(hatchBrush, x, y, w, h)
            
Next i

            
' Clean up.
            font.Dispose()
            hatchBrush.Dispose()
            g.Dispose()

            
' Set the image.
            Me.image = bitmap
        
End Sub
 'GenerateImage


        
''取得4位随机码
        'Public Shared Function GenerateRandomCode() As String
        '    Dim s As String = ""
        '    Dim random As New Random
        '    Dim i As Integer
        '    For i = 0 To 5
        '        s = [String].Concat(s, random.Next(10).ToString())
        '    Next i
        '    Return s
        'End Function 'GenerateRandomCode
        Private Sub SetDimensions(ByVal width As IntegerByVal height As Integer)
            
' Check the width and height.
            If width <= 0 Then
                
Throw New ArgumentOutOfRangeException("width", width, "Argument out of range, must be greater than zero.")
            
End If
            
If height <= 0 Then
                
Throw New ArgumentOutOfRangeException("height", height, "Argument out of range, must be greater than zero.")
            
End If
            
Me.width = width
            
Me.height = height
        
End Sub
 'SetDimensions


        
' ====================================================================
        ' Sets the font used for the image text.
        ' ====================================================================
        Private Sub SetFamilyName(ByVal familyName As String)
            
' If the named font is not installed, default to a system font.
            Try
                
Dim font As New Font(Me.familyName, 12.0F)
                
Me.familyName = familyName
                font.Dispose()
            
Catch ex As Exception
                
Me.familyName = System.Drawing.FontFamily.GenericSerif.Name
            
End Try
        
End Sub
 'SetFamilyName


      

    
End Class
 'Attachment 
End Class

调用

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

    
Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
        Session(
"xmWgLoginimg"= ""
        
'在此处放置初始化页的用户代码
        Dim BitmapIMG As New loginImg.loginImg
        BitmapIMG.addimg(GetNum(), 
15050"Arial")
        
Me.Response.Clear()
        
Me.Response.ContentType = "image/jpeg"
        
' Write the image to the response stream in JPEG format.
        BitmapIMG.image.Save(Me.Response.OutputStream, ImageFormat.Jpeg)
        
' Dispose of the CAPTCHA image object.
        BitmapIMG.image.Dispose()
    
End Sub

    
Private Function GetNum() As String
        
'Dim rnd As New Random
        'Dim r As Integer = rnd.Next(System.Convert.ToInt32(Math.Pow(10, 4 - 1)), System.Convert.ToInt32(Math.Pow(10, 4)))
        'Session("xmWgLoginimg") = r.ToString
        'Return r.ToString()
        Dim Vchar As String = "2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,R,S,T,U,W,X,Y"
        
Dim VcArray() As String = Split(Vchar, ",")
        
Dim VNum As String = ""
        
Dim i As Byte
        
For i = 1 To 5
            
Randomize()
            VNum 
= VNum & VcArray(Int(29 * Rnd()))
        
Next
        
Return VNum

    
End Function
 'GetNum
End Class

验证码的I和1很难分清,建议大家做验证码把这二个都去掉

抱歉!评论已关闭.