另外vb.net数字验证码,还有一种方法是将所有的字符和数字做成不同的小图片进行拼接而成的一个字符串序列;
小图片要自己去做;效果就可以任意展现了 。
求一个vb.net写的验证码?【vb.net数字验证码 visual studio验证码】建一个YanzhenCard.aspx文件
保留%@ Page Language="VB" AutoEventWireup="false" CodeFile="YanzhenCard.aspx.vb" Inherits="YanzhenCard" %,其余删除
如下是代码:
Imports System.IO
Imports System.Drawing
Partial Class YanzhenCard
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim VNum As String = GenerateRandom(4)
Session("VNum") = VNum
ValidateCode(VNum)
End If
End Sub
Private Sub ValidateCode(ByVal VNum As String)
Dim Gheight As Integer = CType((VNum.Length * 15), Integer)
Dim Img As Bitmap = New Bitmap(Gheight, 20)'Bitmap是用于处理由像素数据定义的图像
Dim g As Graphics = Graphics.FromImage(Img)'Graphics.FromImage 从指定的Image创建新的Graphics
g.DrawString(VNum, New Font("Arial", 12), New SolidBrush(Color.Red), 5, 0)'g.DrawString()在指定位置并且用指定的Brush和Font对象绘制指定的文本字符串 。
Dim ms As MemoryStream = New MemoryStream'创建其支持存储区为内存的流 。
Img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
Response.ClearContent()
Response.ContentType = "image/PNG"
Response.BinaryWrite(ms.ToArray)
Response.Write(VNum)
g.Dispose()
Img.Dispose()
Response.End()
End Sub
Private Shared constant As Char() = {"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"}
Public Shared Function GenerateRandom(ByVal Length As Integer) As String
Dim newRandom As System.Text.StringBuilder = New System.Text.StringBuilder(26)
Dim rd As Random = New Random
Dim i As Integer = 0
While iLength
newRandom.Append(constant(rd.Next(26)))
System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
End While
Return newRandom.ToString
End Function
End Class
调用方法:asp:ImageButton ID="Yanzhengma_Card" runat="server" ImageUrl="YanzhenCard.aspx" Height="20px" Width="75px" BorderColor="#6699cc" BorderStyle="Solid" BorderWidth="1px" /
VB NET 中怎样实现动态验证码Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
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.Drawing
Partial Public Class VerifyCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Session("VerifyCode") = MakeValidateCode()
CreateImage(Session("VerifyCode").ToString())
End Sub
Private Sub CreateImage(ByVal checkCode As String)
Dim iwidth As Integer = CInt(checkCode.Length * 12) + 10
Dim image As 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 New Random()
'随机输出噪点
For i As Integer = 0 To 4
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)
Next i
推荐阅读
- 基础游戏开发,游戏开发需要掌握什么
- oracle数据导入导出详细步骤,oracle 数据导入
- 虚拟机移除硬盘怎么办,vmware虚拟机移除
- 大树直播间录屏怎么录,大树直播间录屏怎么录的
- go语言创建文件 go语言文件名
- redis的token令牌,Redis令牌池
- chatgpt复盘报告,复盘分析
- mongodbcount去重复数据库,mongodb 去重
- 怎么看当前mysql密码 查看mysql密码