当前位置: 代码迷 >> JavaScript >> 网页注册验证码如何生成和怎么刷新啊
  详细解决方案

网页注册验证码如何生成和怎么刷新啊

热度:172   发布时间:2012-04-12 15:46:35.0
网页注册验证码怎么生成和如何刷新啊??
网页注册验证码怎么生成和如何刷新啊??

------解决方案--------------------
生成用服务端语言,比如以下是我用asp.net/vb写的
VB.NET code

Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging

Partial Class getpassimgcode
    Inherits System.Web.UI.Page


    Dim rand As New Random()
    Dim r As Byte, g As Byte, b As Byte

    Dim imagebackgroundbrightness As Byte = 162
 

    Function gethightcontrastcolor(Optional ByVal brightness1 As Byte = 255, Optional ByVal dif As Byte = 60) As Color
        r = rand.Next(0, 256)
        g = rand.Next(0, 256)
        b = rand.Next(0, 256)

        While Math.Abs(getcolorbrightness(r, g, b) - brightness1) < dif
            r = rand.Next(0, 256)
            g = rand.Next(0, 256)
            b = rand.Next(0, 256)
        End While
        Return Color.FromArgb(r, g, b)
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim VNum As String = Right(Rnd().ToString, 5)
        Session("VNum") = VNum
        If Not Request.UrlReferrer Is Nothing AndAlso Request.UrlReferrer.ToString.IndexOf("userlogin.aspx") > -1 Then imagebackgroundbrightness = 255
        ValidateCode(VNum)
    End Sub

    '生成图象验证码函数

    Sub ValidateCode(ByVal VNum As String)
        Dim Img As System.Drawing.Bitmap
        Dim g As Graphics
        Dim ms As MemoryStream
        Dim gheight As Integer = Int(Len(VNum) * 11.5)
        'gheight为图片宽度,根据字符长度自动更改图片宽度
        Img = New Bitmap(gheight, 20)
        g = Graphics.FromImage(Img)




        For i As Int16 = 1 To Len(VNum)
            g.DrawString(Mid(VNum, i, 1), New Font("Arial", 10, FontStyle.Bold), New SolidBrush(gethightcontrastcolor(imagebackgroundbrightness)), (i - 1) * 10 + 3, 3)
        Next
        ms = New MemoryStream()
        Img.Save(ms, ImageFormat.Png)
        Response.ClearContent() '需要输出图象信息 要修改HTTP头
        Response.ContentType = "image/Png"
        Response.BinaryWrite(ms.ToArray())
        g.Dispose()
        Img.Dispose()
        Response.End()
    End Sub



    Function getcolorbrightness(ByVal r As Int16, ByVal g As Int16, ByVal b As Int16) As Double
        Return (299 * r + 587 * g + 114 * b) / 1000
    End Function
End Class

------解决方案--------------------
探讨

生成用服务器端语言生成后比如存成 getcodeimg.asp
然后调用页就可以<img src=" getcodeimg.asp" id="img1" />
刷新<a onclick=' document.getElementById("img").src = "getcodeimg.asp?" + Math.random()'>看不清?换一个<a/>
  相关解决方案