您好,登录后才能下订单哦!
本篇文章给大家分享的是有关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.BitmapDim 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.LinearGradientBrushbrush = 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)
Dim ms1 As System.IO.MemoryStream
ms1 = New System.IO.MemoryStream
img.Save(ms1, System.Drawing.
Imaging.ImageFormat.Png)Response.ClearContent()
'需要输出图象信息 要修改HTTP头Response.ContentType = "image/Png"
Response.BinaryWrite(ms1.ToArray())
g.Dispose()
img.Dispose()
Response.End()
End Sub
函数名称: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*rndvnumvnum = vnum & vcarray(Int(29 *
Rnd())) '数组一般从0开始读取,所以这里为35*rndNext
Return vnum
End Function
以上就是VB.NET中怎么生成验证码,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。