用vb.net实现验证码生成程序

首先引入命名空间:

None.gif Imports  System.Drawing
None.gif
Imports  System.io

 

  ' 产生随机码
ExpandedBlockStart.gifContractedBlock.gif
     Private   Function generateVCode() Function generateVCode(ByVal CodeLength As IntegerAs String
InBlock.gif        
Dim allChar As String = "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,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"
InBlock.gif        
Dim allCharArray() As String = allChar.Split(",")
InBlock.gif        
Dim randomCode As String = ""
InBlock.gif        
Dim temp As Integer = -1
InBlock.gif        
Dim rand As Random = New Random
InBlock.gif        
Dim i As Integer = 0
InBlock.gif        
Do While (i < CodeLength)
InBlock.gif            
If (temp <> -1Then
InBlock.gif                
Dim aa As Integer = CType(DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer'根据时间生成随机数种子
InBlock.gif
                rand = New Random(aa)
InBlock.gif            
End If
InBlock.gif            
Dim t As Integer = rand.Next(61+ 1
InBlock.gif            
If t > allCharArray.Length - 1 Then t = allCharArray.Length - 1
InBlock.gif            
If temp = t Then '抑制产生连续重复的验证码。
InBlock.gif

InBlock.gif                i 
-= 1
InBlock.gif                randomCode 
= Microsoft.VisualBasic.Left(randomCode, i)
InBlock.gif            
End If
InBlock.gif            temp 
= t
InBlock.gif            randomCode 
= randomCode + allCharArray(t)
InBlock.gif            i 
+= 1
InBlock.gif        
Loop
InBlock.gif        
Return randomCode
ExpandedBlockEnd.gif    
End Function

None.gif   ' 创建验证码图象
ExpandedBlockStart.gifContractedBlock.gif
     Private   Sub CreateImage() Sub CreateImage(ByVal checkCode As String)
InBlock.gif        
Dim iwidth As Integer = Convert.ToInt32(checkCode.Length * 15)
InBlock.gif        
Dim image As Bitmap = New Bitmap(iwidth, 25)
InBlock.gif        
Dim g As Graphics = Graphics.FromImage(image)
InBlock.gif        
Dim f As Font = New Font("Arial"12, System.Drawing.FontStyle.Bold) '设置输出的字体的及字大小可作相应调整
InBlock.gif
        Dim b As Brush = New SolidBrush(Color.White)
InBlock.gif        g.Clear(Color.Green) 
'填充背景色,可根据页面风格更换相应的颜色
InBlock.gif
        g.DrawString(checkCode, f, b, 33)
InBlock.gif        
Dim rand As Random = New Random
InBlock.gif        
'画图片的前景燥点
InBlock.gif
        For i As Integer = 0 To 60
InBlock.gif            
Dim x As Integer = rand.Next(image.Width)
InBlock.gif            
Dim y As Integer = rand.Next(image.Height)
InBlock.gif            image.SetPixel(x, y, Color.FromArgb(rand.Next()))
InBlock.gif        
Next
InBlock.gif        
Dim ms As MemoryStream = New System.IO.MemoryStream
InBlock.gif        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
InBlock.gif        Response.ClearContent()
InBlock.gif        Response.ContentType 
= "image/Jpeg"
InBlock.gif        Response.BinaryWrite(ms.ToArray())
InBlock.gif        g.Dispose()
InBlock.gif        image.Dispose()
ExpandedBlockEnd.gif    
End Sub

ExpandedBlockStart.gif ContractedBlock.gif     Protected   Sub Page_Load() Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
InBlock.gif        
'在此处放置初始化页的用户代码
InBlock.gif
        Dim checkCode As String = generateVCode(4)
InBlock.gif        Session(
"CheckCode"= checkCode.ToString
InBlock.gif        CreateImage(checkCode)
ExpandedBlockEnd.gif    
End Sub
现在我把它封装成了web控件, 需要的朋友可以直接拿去用,但还请各位朋友能够帮助改进程序,谢谢!
验证控件
如果无法下载,请发邮件至 wxnet2008@gmail.com

转载于:https://www.cnblogs.com/wangxiang/articles/615162.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值