-
生成验证码(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
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
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式