做登陆页面的随机验证码

先放上这页代码:(radamdata.aspx是用来产生验证码图片用的)

Imports System.Drawing
Imports System.IO
Partial Class test2
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' 在此处放置用户代码以初始化页面
        If Not Page.IsPostBack Then
            Me.GenImg(Me.GenCode(4))
 
        End If
    End Sub
 
    Private Function GenCode(ByVal num As Integer) As String
        Dim [source] 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"}
        Dim code As String = ""
        Dim rd As New Random()
        Dim i As Integer
        For i = 0 To num - 1
            code += [source](rd.Next(0, [source].Length))
        Next i
        Return code
    End Function 'GenCode
 
 
    ' 生成图片
    Private Sub GenImg(ByVal code As String)
        Dim myPalette As New Bitmap(60, 20) ' 定义一个画板
        Dim gh As Graphics = Graphics.FromImage(myPalette) ' 在画板上定义绘图的实例
        Dim rc As New Rectangle(0, 0, 60, 20) ' 定义一个矩形
        gh.FillRectangle(New SolidBrush(Color.Silver), rc) ' 填充矩形
        gh.DrawString(code, New Font(" 宋体" , 16), New SolidBrush(Color.Gray), rc) ' 在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
        myPalette.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg) ' 将图片显示出来
        ' 下面这些也是将图片显示出来的!
        'Dim ms As MemoryStream = New MemoryStream
        'myPalette.Save(ms, Imaging.ImageFormat.Jpeg)
        ' Response.ClearContent()
        ' Response.ContentType = "image/jpeg"
        ' Response.BinaryWrite(ms.ToArray)
        Session("ValidateCode") = code ' 将字符串保存到Session中,以便需要时进行验证
        gh.Dispose()
        myPalette.Dispose()
    End Sub 'GenImg
 
 
End Class

 ..................................................................................................................................................................

放上调用radamdata.aspx的radamindex.aspx的代码

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = Session("ValidateCode") Then
            Label1.Text = "haha, 你成功了!!!"
        Else
            Label1.Text = " 不对哦"
        End If
    End Sub
End Class
然后在randamindex.aspx上放一个image控件,使他的图片连接为randamdata.aspx就行了!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值