温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

VB.NET中怎么生成验证码

发布时间:2021-07-20 11:41:39 来源:亿速云 阅读:129 作者:Leah 栏目:编程语言

本篇文章给大家分享的是有关VB.NET中怎么生成验证码,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

VB.NET生成验证码前台代码为:

  1. < asp:image id="Image2" 
    runat="server" ImageUrl=
    "ValidateCode.aspx"> 

ValidateCode.aspx文件的代码为:

  1. Private Sub Page_Load
    (ByVal sender As System.Object, 
    ByVal e As System.EventArgs)
     Handles MyBase.Load  

  2. list()  

  3. End Sub  

  4. Function list() 

在此处放置初始化页的用户代码,rndnum是一个自定义函数

  1. Dim vnum As String 
    = rndnum(4)  

  2. Session("vnum") = vnum  

  3. validatecode(vnum)  

  4. End Function 

VB.NET生成验证码生成图象验证码函数

  1. Sub validatecode(ByVal vnum)  

  2. Dim img As System.
    drawing.Bitmap  

  3. Dim g As Graphics  

  4. Dim r As Random = New Random  

  5. Dim gheight As IntInteger = 
    Int(Len(vnum) * 13) 

gheight为图片宽度,根据字符长度自动更改图片宽度

  1. img = New Bitmap(gheight, 20)  

  2. g = Graphics.FromImage(img)  

  3. 'g.DrawString(vnum, New System.
    Drawing.Font
    ("Arial", 10), New System.Drawing.
    SolidBrush(Color.Blue), 3, 3) 

新增,修改、画图片的背景噪音线

  1. 'For i As Integer = 0 To 25  

  2. For i As Integer = 0 To 10  

  3. Dim x1 As Integer  

  4. x1 = r.Next(img.Width)  

  5. Dim x2 As Integer = 
    r.Next(img.Width)  

  6. Dim y1 As Integer = 
    r.Next(img.Height)  

  7. Dim y2 As Integer = 
    r.Next(img.Height)  

  8. g.DrawLine(New Pen(Color.Silver), 
    x1, y1, x2, y2)  

  9. Next i  

  10. Dim font As font  

  11. font = New System.Drawing.Font
    ("Arial", 12)  

  12. Dim brush As System.Drawing.
    Drawing2D.LinearGradientBrush  

  13. brush = New System.Drawing.
    Drawing2D.LinearGradientBrush
    (New Rectangle(0, 0, img.Width, 
    img.Height), Color.Blue, Color.
    Blue, 1.2F, True)  

  14. g.DrawString(vnum, font, brush, 2, 2) 

VB.NET生成验证码中画图片的前景噪音点

  1. 'For ii As Integer = 
    0 To 100  

  2. ' Dim x As Integer = 
    r.Next(img.Width)  

  3. ' Dim y As Integer = 
    r.Next(img.Height)  

  4. ' img.SetPixel(x, y, 
    Color.FromArgb(r.Next()))  

  5. 'Next 

画图片的边框线

  1. g.DrawRectangle
    (New Pen(Color.Silver), 
    0, 0, img.Width - 1, 
    img.Height - 1) 

在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)

  1. Dim ms1 As System.IO.MemoryStream  

  2. ms1 = New System.IO.MemoryStream  

  3. img.Save(ms1, System.Drawing.
    Imaging.ImageFormat.Png)  

  4. Response.ClearContent() 
    '需要输出图象信息 要修改HTTP头   

  5. Response.ContentType = "image/Png" 

  6. Response.BinaryWrite(ms1.ToArray())  

  7. g.Dispose()  

  8. img.Dispose()  

  9. Response.End()  

  10. End Sub 


函数名称:rndnum

函数参数:vcodenum--设定返回随机字符串的位数

函数功能:产生数字和字符混合的随机字符串

  1. Function rndnum(ByVal vcodenum)  

  2. '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" 

  3. 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" 

  4. Dim vcarray() As String = Split
    (vchar, ",") '将字符串生成数组  

  5. Dim vnum As String = "" 

  6. Dim i As Byte  

  7. For i = 1 To vcodenum  

  8. Randomize()  

  9. 'vnumvnum = vnum & vcarray(Int(35 * 
    Rnd())) '数组一般从0开始读取,所以这里为35*rnd  

  10. vnumvnum = vnum & vcarray(Int(29 * 
    Rnd())) '数组一般从0开始读取,所以这里为35*rnd  

  11. Next  

  12. Return vnum  

  13. End Function 

以上就是VB.NET中怎么生成验证码,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI