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


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

  • 相关阅读:
    事务隔离级别
    OpenSessionInView
    图像平滑处理(滤波)
    [原]Nginx+Lua服务端合并静态文件
    检查数据倾斜分布
    SQL Server研究之统计信息—发现过期统计信息并处理具体解释
    GDALWarp设置GDALWarpOptions::dfWarpMemoryLimit过大时处理失败
    Android Studio 2.0 稳定版新特性介绍
    供应商和管理员查看供应商地址簿信息SQL
    Table AdvanceTable针对表内行禁用multipleSelection , singleSelection
  • 原文地址:https://www.cnblogs.com/wangxiang/p/795522.html
Copyright © 2011-2022 走看看