本篇文章给大家分享的是有关VB.NET中怎么生成验证码,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。VB.net生成验证码前台代码为:< asp:image&n
本篇文章给大家分享的是有关VB.NET中怎么生成验证码,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
VB.net生成验证码前台代码为:
< asp:image id="Image2"
runat="server" ImageUrl=
"ValidateCode.aspx">
ValidateCode.aspx文件的代码为:
Private Sub Page_Load
(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles MyBase.Load
list()
End Sub
Function list()
在此处放置初始化页的用户代码,rndnum是一个自定义函数
Dim vnum As String
= rndnum(4)
Session("vnum") = vnum
validatecode(vnum)
End Function
VB.NET生成验证码生成图象验证码函数
Sub validatecode(ByVal vnum)
Dim img As System.
drawing.Bitmap
Dim g As Graphics
Dim r As Random = New Random
Dim gheight As IntInteger =
Int(Len(vnum) * 13)
gheight为图片宽度,根据字符长度自动更改图片宽度
img = New Bitmap(gheight, 20)
g = Graphics.FromImage(img)
'g.DrawString(vnum, New System.
Drawing.Font
("Arial", 10), New System.Drawing.
SolidBrush(Color.Blue), 3, 3)
新增,修改、画图片的背景噪音线
'For i As Integer = 0 To 25
For i As Integer = 0 To 10
Dim x1 As Integer
x1 = r.Next(img.Width)
Dim x2 As Integer =
r.Next(img.Width)
Dim y1 As Integer =
r.Next(img.Height)
Dim y2 As Integer =
r.Next(img.Height)
g.DrawLine(New Pen(Color.Silver),
x1, y1, x2, y2)
Next i
Dim font As font
font = New System.Drawing.Font
("Arial", 12)
Dim brush As System.Drawing.
Drawing2D.LinearGradientBrush
brush = New System.Drawing.
Drawing2D.LinearGradientBrush
(New Rectangle(0, 0, img.Width,
img.Height), Color.Blue, Color.
Blue, 1.2F, True)
g.DrawString(vnum, font, brush, 2, 2)
VB.NET生成验证码中画图片的前景噪音点
'For ii As Integer =
0 To 100
' Dim x As Integer =
r.Next(img.Width)
' Dim y As Integer =
r.Next(img.Height)
' img.SetPixel(x, y,
Color.FromArgb(r.Next()))
'Next
画图片的边框线
g.DrawRectangle
(New Pen(Color.Silver),
0, 0, img.Width - 1,
img.Height - 1)
在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
函数名称:rndnum
函数参数:vcodenum--设定返回随机字符串的位数
函数功能:产生数字和字符混合的随机字符串
Function rndnum(ByVal vcodenum)
'Dim vchar 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,W,X,Y,Z"
Dim vchar As String = "2,3,4,5,6,7,
8,9,A,B,C,D,E,F,G,H,J,K,L,N,P,Q,
R,S,T,U,X,Y,Z"
Dim vcarray() As String = Split
(vchar, ",") '将字符串生成数组
Dim vnum As String = ""
Dim i As Byte
For i = 1 To vcodenum
Randomize()
'vnumvnum = vnum & vcarray(Int(35 *
Rnd())) '数组一般从0开始读取,所以这里为35*rnd
vnumvnum = vnum & vcarray(Int(29 *
Rnd())) '数组一般从0开始读取,所以这里为35*rnd
Next
Return vnum
End Function
以上就是VB.NET中怎么生成验证码,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网精选频道。
--结束END--
本文标题: VB.NET中怎么生成验证码
本文链接: https://lsjlt.com/news/291516.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0