zoukankan      html  css  js  c++  java
  • 生成验证码(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 * 13Integer)
            
    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, 11)
                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 + 1Mod 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), 00, 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 IntegerAs 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 <> -1Then
                    
    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


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

  • 相关阅读:
    定时器中断彩灯控制程序
    单片机C51 8位流水灯
    十进制转换2-9进制转换
    加减乘除+菜单实现
    文件 I/O缓冲流
    文件 I/O字符流
    spring配置c3p0连接池
    javax.swing.Timer的与Lambda的使用
    代码简化之道--接口之从传统实现到Lambda表达式实现
    Java核心技术第六章--内部类
  • 原文地址:https://www.cnblogs.com/wangxiang/p/795522.html
Copyright © 2011-2022 走看看