生成验证码(vb.net)

生成图片验证码的代码(vb.net版)
Imports  System
Imports  System.Data
Imports  System.Configuration
Imports  System.Web
Imports  System.Web.Security
Imports  System.Web.UI
Imports  System.Web.UI.WebControls
Imports  System.Web.UI.WebControls.WebParts
Imports  System.Web.UI.HtmlControls
Imports  System.IO
Imports  System.Drawing
' 生成图片验证码类
Public   Class  CreateImage
    
Public   Sub  CreateImage()

    
End Sub
    
Public   Shared   Sub  DrawImage()
        
Dim  img  As  CreateImage  =   New  CreateImage()
        HttpContext.Current.Session(
" CheckCode " =  Global.CreateImage.RndNum( 4 )
        Global.CreateImage.CreateImages(HttpContext.Current.Session(
" CheckCode " ).ToString())

    
End Sub
    
' '' <summary>
     ' '' 生成验证图片
     ' ''</summary>
     ' ''<param name="checkCode">验证字符</param>
     Public   Shared   Sub  CreateImages( ByVal  checkCode  As   String )
        
Dim  iwidth  As   Integer   =   CType (checkCode.Length  *   13 Integer )
        
Dim  image  As  System.Drawing.Bitmap  =   New  System.Drawing.Bitmap(iwidth,  25 )

        
Dim  g  As  Graphics  =  Graphics.FromImage(image)
        g.Clear(Color.White)
        
' 定义颜色
         Dim  c  As  Color()  =  {Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple}
        
' 定义字体
         Dim  font  As   String ()  =  { " Verdana " " Microsoft Sans Serif " " Comic Sans MS " " Arial " " 宋体 " }
        
Dim  rand  As  Random  =   New  Random()
        
Dim  i  As   Integer   =   0
        
Do   While  i  <   50
            
Dim  x  As   Integer   =  rand.Next(image.Width)
            
Dim  y  As   Integer   =  rand.Next(image.Height)
            g.DrawRectangle(
New  Pen(Color.LightGray,  0 ), x, y,  1 1 )
            i 
+=   1

        
Loop
        i 
=   0
        
Do   While  i  <  checkCode.Length
            
Dim  cindex  As   Integer   =  rand.Next( 7 )
            
Dim  findex  As   Integer   =  rand.Next( 5 )
            
Dim  f  As  Font  =   New  System.Drawing.Font(font(findex),  10 , System.Drawing.FontStyle.Bold)
            
Dim  b  As  Brush  =   New  System.Drawing.SolidBrush(c(cindex))
            
Dim  ii  As   Integer   =   4
            
If  (i  +   1 Mod   2   =   0   Then
                ii 
=   2
            
End   If
            g.DrawString(checkCode.Substring(i, 
1 ), f, b,  3   +  (i  *   12 ), ii)
            i 
+=   1
        
Loop
        
' 画一个边框
        g.DrawRectangle( New  Pen(Color.Black,  0 ),  0 0 , image.Width  -   1 , image.Height  -   1 )
        
Dim  ms  As  System.IO.MemoryStream  =   New  System.IO.MemoryStream()
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
        HttpContext.Current.Response.ClearContent()
        HttpContext.Current.Response.ContentType 
=   " image/Jpeg "
        HttpContext.Current.Response.BinaryWrite(ms.ToArray())
        g.Dispose()
        image.Dispose()
    
End Sub

    
' '' <summary>
     ' '' 生成随机的字母
     ' '' </summary>
     ' '' <param name="VcodeNum">生成字母的个数</param>
     ' '' <returns>string</returns>
     Public   Shared   Function  RndNum( ByVal  VcodeNum  As   Integer As   String
        
Dim  allChar  As   String   =   " 0,1,2,3,4,5,6,7,8,9 "
        
Dim  allCharArray()  As   String   =  allChar.Split( " , " )
        
Dim  randomCode  As   String   =   ""
        
Dim  temp  As   Integer   =   - 1
        
Dim  rand  As  Random  =   New  Random
        
Dim  i  As   Integer   =   0
        
Do   While  (i  <  VcodeNum)
            
If  (temp  <>   - 1 Then
                
Dim  aa  As   Integer   =   CType (DateTime.Now.Ticks  Mod  System.Int32.MaxValue,  Integer ' 根据时间生成随机数种子
                rand  =   New  Random(aa)
            
End   If
            
Dim  t  As   Integer   =  rand.Next( 61 +   1
            
If  t  >  allCharArray.Length  -   1   Then  t  =  allCharArray.Length  -   1
            
If  temp  =  t  Then   ' 抑制产生连续重复的验证码。

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


生成图片验证码,请指教!

转载于:https://www.cnblogs.com/wangxiang/archive/2007/06/25/795522.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值