VB,NET 验证码控件
来源:互联网 发布:数据库客户端 编辑:程序博客网 时间:2024/06/06 01:01
验证码是用来防止程序反复登陆系统造成的损失的一个实用的小措施,
而采用验证码控件能够很方便的使用验证码
下面就简单讲述一下一个简单的验证码控件的开发过程。
一个验证码控件的组成部分如下:
1.输入框,用于输入验证码 在这里使用textbox
2.图片框,用于显示验证码 在这里使用panel
3.标志框,用于显示验证码是否正确,在这里用label显示
以上是控件部分,简便起见,控件名全部采用默认值,实际应用中请自行修改;下面说一下代码中的部分,
首先是私有字段
包括:
1._Ispass as boolean 逻辑型变量,指示验证是否通过。
2._Key as string 字符串型变量,存储验证码,
3.KeyString as string() 字符串数组,存储0-9,A-Z。a-z,共62个字符。
然后是属性,
这里只有一个只读的boolean型,IsPass 指示是否通过验证。
一个事件 KeyVerification 通过验证时触发
两个方法:
1. Public Sub Refresh 刷新验证码
2.Private Sub Textbox1_changed textbox内容变更时触发,判断验证码是否一致
代码如下 :
Public Class Verification
Private _IsPass As Boolean
Private _Key As String = ""
Public Event PassVerification(sender As Object, e As EventArgs)
Dim KeyString As String() = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"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" _
, "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "n", "m", "o",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
Public ReadOnly Property IsPass As Boolean
Get
Return _IsPass
End Get
End Property
Public Sub Refersh(sender As Object, e As EventArgs) Handles TextBox1.GotFocus, Panel1.Click, Label1.Click
'Dim Key As String = ""
_Key = ""
Randomize()
For i = 0 To 3
_Key += KeyString(Int(Rnd() * 52 + 1))
Next
Dim g As Graphics = Me.Panel1.CreateGraphics
g.Clear(SystemColors.Control)
g.DrawString(Mid(_Key, 1, 1), New Font("宋体", 20, FontStyle.Bold), Brushes.Red, New Point(2, 2))
g.DrawString(Mid(_Key, 2, 1), New Font("宋体", 20, FontStyle.Bold), Brushes.Red, New Point(22, 2))
g.DrawString(Mid(_Key, 3, 1), New Font("宋体", 20, FontStyle.Bold), Brushes.Red, New Point(42, 2))
g.DrawString(Mid(_Key, 4, 1), New Font("宋体", 20, FontStyle.Bold), Brushes.Red, New Point(62, 2))
'噪声线
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.Dispose()
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.ToLower = _Key.ToLower Then
Label1.Text = "√"
Label1.ForeColor = Color.Green
RaiseEvent PassVerification(Me, New EventArgs)
Else
Label1.Text = "×"
Label1.ForeColor = Color.Red
End If
End Sub
End Class
Private _IsPass As Boolean
Private _Key As String = ""
Public Event PassVerification(sender As Object, e As EventArgs)
Dim KeyString As String() = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"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" _
, "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "n", "m", "o",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
Public ReadOnly Property IsPass As Boolean
Get
Return _IsPass
End Get
End Property
Public Sub Refersh(sender As Object, e As EventArgs) Handles TextBox1.GotFocus, Panel1.Click, Label1.Click
'Dim Key As String = ""
_Key = ""
Randomize()
For i = 0 To 3
_Key += KeyString(Int(Rnd() * 52 + 1))
Next
Dim g As Graphics = Me.Panel1.CreateGraphics
g.Clear(SystemColors.Control)
g.DrawString(Mid(_Key, 1, 1), New Font("宋体", 20, FontStyle.Bold), Brushes.Red, New Point(2, 2))
g.DrawString(Mid(_Key, 2, 1), New Font("宋体", 20, FontStyle.Bold), Brushes.Red, New Point(22, 2))
g.DrawString(Mid(_Key, 3, 1), New Font("宋体", 20, FontStyle.Bold), Brushes.Red, New Point(42, 2))
g.DrawString(Mid(_Key, 4, 1), New Font("宋体", 20, FontStyle.Bold), Brushes.Red, New Point(62, 2))
'噪声线
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))
g.Dispose()
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.ToLower = _Key.ToLower Then
Label1.Text = "√"
Label1.ForeColor = Color.Green
RaiseEvent PassVerification(Me, New EventArgs)
Else
Label1.Text = "×"
Label1.ForeColor = Color.Red
End If
End Sub
End Class
这段代码十分简陋,
在实际应用中,各位可以采用一些手段来提高这个控件的防识别功能,并提高性能,
比如说在绘制验证码的可以旋转,扭曲,变换字体粗细,颜色,变换噪声线颜色,粗细,添加噪声点,等等方法
在验证中也可以通过下列代码来提高一点点性能
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
if Textbox1.text.length <> _key.length then return-----------------------------------------------------------------------------添加这一句来使长度不等于验证码的跳过判断相等
if Textbox1.text.length <> _key.length then return-----------------------------------------------------------------------------添加这一句来使长度不等于验证码的跳过判断相等
If TextBox1.Text.ToLower = _Key.ToLower Then
Label1.Text = "√"
Label1.ForeColor = Color.Green
RaiseEvent PassVerification(Me, New EventArgs)
Else
Label1.Text = "×"
Label1.ForeColor = Color.Red
End If
End Sub
Label1.Text = "√"
Label1.ForeColor = Color.Green
RaiseEvent PassVerification(Me, New EventArgs)
Else
Label1.Text = "×"
Label1.ForeColor = Color.Red
End If
End Sub
------------------------------------------------------------------------------------By Lzx AT 16-1-16
0 0
- VB,NET 验证码控件
- 验证码VB.net版
- asp.net 图像验证码 (vb.net)
- .net实现验证码VB版
- VB.net实现图像验证码功能
- ASP.NET(VB) 图片验证码
- ASP.NET(VB) 图片验证码
- Asp.Net生成图片验证码(vb)
- vb.net实现验证码功能
- vb.net 简单的验证码识别
- vb.net 生成验证码图片-转载
- 验证链接(vb.net)
- ASP.NET验证码控件
- vb.net隐藏控件
- VB.NET 控件说明
- [VB.NET]datagridview控件
- [VB.NET]richtextbox控件
- vb.net控件简述
- 饿了么移动APP的架构演进
- Hbase过滤器的一些总结
- 1391: [Ceoi2008]order 最大权闭合图 最小割
- pdf转换
- android make
- VB,NET 验证码控件
- ffmpeg h264 解码 转
- Reactor
- Swift 2.3 【这些情况下guard要慎用】
- redis常见操作命令-string
- Codeforces 609 D Gadgets for dollars and pounds
- 1391: [Ceoi2008]order|网络流
- 在IPCAM上实现RTSP协议直播-live555 转
- 图片阴影效果和影子效果