Asp.Net生成图片验证码(vb)

Validate.vb
代码如下
Imports  System
Imports  System.Drawing.Drawing2D
Imports  System.Drawing.imaging
Imports  System.Drawing
Imports  System.Web.UI
Imports  System.IO
Public   Class Validate

    
Public Class CreatValidateNumber
        
'生成验证码
        Public Function CreatValidateNumber(ByVal length As Integer)
            
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 length
                
Randomize()
                vnum 
= vnum & vcarray(Int(35 * Rnd())) '数组一般从0开始读取,所以这里为35*rnd 
            Next
            
Return vnum
        
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(00, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, True)
                g.DrawString(validatenum, font, brush, 
32)
                
'画前景干扰点
                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), 00, 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 Class



新建一个img.aspx的窗体
后台代码如下
Public   Class img
    
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)
        validates.CreatevalidateGraphic(
Me, vali)
        Session(
"vali"= vali
    
End Sub


End Class


调用的HMTL
< HTML >
    
< HEAD >
        
< title > WebForm1 </ title >
        
< 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" >
            
< asp:Image  id ="Image1"  style ="Z-INDEX: 101; LEFT: 648px; POSITION: absolute; TOP: 256px"  runat ="server"
                ImageUrl
="img.aspx" ></ asp:Image >
            
< asp:TextBox  id ="tb_valide"  style ="Z-INDEX: 102; LEFT: 464px; POSITION: absolute; TOP: 256px"
                runat
="server" ></ asp:TextBox >
            
< asp:Button  id ="Button1"  style ="Z-INDEX: 103; LEFT: 560px; POSITION: absolute; TOP: 328px"  runat ="server"
                Text
="Button" ></ asp:Button >
        
</ form >
    
</ body >
</ HTML >


后台

 

     Private   Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
If tb_valide.Text = Session("vali"Then
            Response.Write(
"<font color='red'>提示:验证通过</font>")
        
Else

            Response.Write(
"所填写的验证码与所给的不符")
        
End If
    
End Sub


完成效果如下

你可能感兴趣的:(asp.net)