<%
'属性:
' SessionName String 会话名称. 例 Object.SessionName = "Code"
' TextLength Int   字符长度. 例 Object.TextLength = 4
' BackColor String 背景颜色,多个颜色用"," 分隔. 例:Object.BackColor = "#FF0000,#00FF00,#0000FF".
' TextColor String 文字颜色,同上.
' YawpColor String 杂点颜色,同上.
' YawpCount Int   杂点比例 范围 0-100. 例 Object.YawpCount = 30
'方法:
' SetTextInfo String 设置字库描述信息 注意在执行任何操作前必须描述和增加点阵数据. 例: Object.SetTextInfo "这是卡通字库"
' AddTextData String 增加字库点阵信息 注意在执行任何操作前必须描述和增加点阵数据. 例: Object.AddTextData "E 00000000 00000000 01111100 01000000 01000000 01111000 01000000 01000000 01000000 01111100 00000000 00000000"
' Write()     输出图片数据
'注意事项:
'1. 关于颜色字符串:
' 可以同时设置任意多种颜色 不同的颜色之间使用 "," 分隔. 程序会将预设的多种颜色随机显示
'2. 关于会话名称:
' SessionName属性设置的是 Session 变量名称.通过在其它页面中将表单数据与此 Session变量值比较,实现验证码功能.
'3. 关于字符长度:
' 字符长度必须大于 1 .最大长度没有限制. 实际上不可能设置很大 "呵呵!ASP毕竟不是画图软件."
'4. 杂点颜色杂点比例:
' 杂点比例范围 0-100. 此值越大杂点越多,0 为无杂点,100全部是杂点.
' 和背景颜色与文字颜色一样 杂点颜色也可以设置一种一上的颜色.程序会随机使用其中的颜色
' 如将杂点比例设置为 0 则 杂点颜色属性可以不设置
'5. 字库信息:
' 字库信息就是一个字符串,此信息必须设置.内容格式不要求
'6. 字库点阵:
' 程序是随机查询字库点阵内容输出BMP图片的.
' 每一条点阵.的格式必须一样.
' 每条点阵信息的长度必须一样.
' 所有点阵信息的行数必须一样.
' 所有点阵信息的每行长度必须一样.
' 点阵信息格式:
'字符 第一行点阵 第二行点阵 第三行点阵 第四行点阵 第五行点阵 第六行点阵 第七行点阵 ...... ...... ..... ...... ......
'0 00000000 00000000 00111000 01000100 01000100 01000100 01000100 01000100 01000100 00111000 00000000 00000000
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 


Class CAMXAM_COM_RndCodeBmp
Private TextDataLib,TextData,BmpData,BmpHead,BmpWidth,BmpHeight,BmpSize,TextDataLength,SessionString
Public SessionName,TextLength,BackColor,TextColor,YawpColor,YawpCount,CharWidth,CharHeight

Private Sub Class_Initialize()
End Sub

Private Sub Class_Terminate()
End Sub

Public Function SetTextInfo(TextString)
    ReDim TextDataLib(0)
    TextDataLib(0) = TextString
End Function

Public Function AddTextData(TextDataString)
    TextDataLength = UBound(TextDataLib) + 1
    ReDim Preserve TextDataLib(TextDataLength)
    TextDataLib(TextDataLength) = TextDataString
End Function

Private Function Getbinary(IntNumber)
    Dim IntBin0,IntBin1,IntBin2,IntBin0_,IntBin1_,IntBin2_
    IntBin0 = Int(IntNumber / 16777216)
    IntBin0_= IntNumber Mod 16777216
    IntBin1 = Int(IntBin0_ / 65536)
    IntBin1_= IntBin0_ Mod 65536
    IntBin2 = Int(IntBin1_ / 256)
    IntBin2_= IntBin1_ Mod 256
    Getbinary = ChrB(IntBin2_) & ChrB(IntBin2) & ChrB(IntBin1) & ChrB(IntBin0)
End Function

Private Function GetColadRstring(ColadRstring)
    Dim ColorArray,CALength
    ColorArray = Split(ColadRstring,",")
    CALength = UBound(ColorArray)
    GetColadRstring = ColorArray(GetRnd(0,CALength))
End Function

Private Function GetRnd(LowNumber,UpNumber)
    Randomize Timer
    GetRnd = Int((UpNumber - LowNumber + 1) * Rnd + LowNumber)
End Function

Private Function FileHeader()
    Response.Expires = -1
    Response.AddHeader "Pragma","no-cache"
    Response.AddHeader "cache-ctrol","no-cache"
    Response.ContentType = "Image/BMP"
End Function

Private Function SetBmpHead(BmpSize,Width,Height)
    BmpHead = ChrB(66) & ChrB(77)
    BmpHead = BmpHead & Getbinary(BmpSize+54)
    BmpHead = BmpHead & Getbinary(0)
    BmpHead = BmpHead & Getbinary(54)
    BmpHead = BmpHead & Getbinary(40)
    BmpHead = BmpHead & Getbinary(Width)
    BmpHead = BmpHead & Getbinary(Height)
    BmpHead = BmpHead & ChrB(1) & ChrB(0)
    BmpHead = BmpHead & ChrB(24) & ChrB(0)
    BmpHead = BmpHead & Getbinary(0)
    BmpHead = BmpHead & Getbinary(BmpSize)
    BmpHead = BmpHead & Getbinary(65536)
    BmpHead = BmpHead & Getbinary(65536)
    BmpHead = BmpHead & Getbinary(16777216)
    BmpHead = BmpHead & Getbinary(16777216)
End Function

 
Private Function GetRndTextData()
    ReDim TextData(TextLength-1)
    Dim IText
    For IText = 0 To TextLength-1 Step +1
        TextData(IText) = GetColadRstring(BackColor) & " " & GetColadRstring(TextColor) & " " &  TextDataLib(GetRnd(1,TextDataLength))
        SessionString = SessionString & GetTextData(IText)(4)
    Next
End Function

Private Function GetTextData(IText)
    Dim TextArray,ArrayLength,ITextData
    TextArray = Split(TextData(IText)," ")
    ArrayLength = UBound(TextArray)
    Dim TextDataArray(5)
    TextDataArray(0) = TextArray(0)
    TextDataArray(1) = TextArray(1)
    TextDataArray(2) = Len(TextArray(3))
    TextDataArray(3) = ArrayLength - 2
    TextDataArray(4) = TextArray(2)
    For ITextData = 3 To ArrayLength Step +1
        TextDataArray(5) = TextDataArray(5) & " " & TextArray(ITextData)
    Next
    GetTextData = TextDataArray
End Function

Private Function GetColor(ColadRstring)
    GetColor = ChrB("&H" & Mid(ColadRstring,6,2)) & ChrB("&H" & Mid(ColadRstring,4,2)) & ChrB("&H" & Mid(ColadRstring,2,2))
End Function

Private Function BmpColor(ColorA,ColorB)
    Dim BmpColor_(1)
    BmpColor_(0) = GetColor(ColorA)
    BmpColor_(1) = GetColor(ColorB)
    BmpColor = BmpColor_
End Function

Private Function SetBmpData()
    Dim TextInfo,FontData,I,J,K
    GetRndTextData()
    TextInfo = GetTextData(0)
    BmpHeight = TextInfo(3)
    BmpWidth = TextInfo(2) * TextLength
    BmpSize = BmpHeight * BmpWidth * 5
    For I = BmpHeight To 1 Step -1
        For J = 0 To TextLength -1 Step +1
            TextInfo = GetTextData(J)
            FontData = Split(TextInfo(5)," ")(I)
            For K = 1 To TextInfo(2) Step +1
                If GetRnd(1,99) < YawpCount Then
                    BmpData = BmpData & GetColor(GetColadRstring(YawpColor))
                Else
                    BmpData = BmpData & BmpColor(TextInfo(0),TextInfo(1))(Mid(FontData,K,1))
                End If
            Next
        Next
    Next
End Function

Public Function Write()
    FileHeader()
    SetBmpData()
    SetBmpHead BmpSize,BmpWidth,BmpHeight
    Response.BinaryWrite BmpHead
    Response.BinaryWrite BmpData
    Session(SessionName) = SessionString
End Function
 

Public Function Fill(psChar, pnWidth)
    Dim nTemp, sTemp
    For nTemp =1 To pnWidth
        sTemp =sTemp & psChar
    Next
    Fill =sTemp
End Function


End Class


Dim Bmp
Set Bmp = New CAMXAM_COM_RndCodeBmp

Bmp.SetTextInfo "0123456789-01234"
Bmp.AddTextData "0 0000000000000000000000 0000000000011111111100 0000000001110000011100 0000000000111000000011100 0000000001100000000001100 0000000011100000000001100 0000000111000000000001100 0000001110000000000001100 0000011110000000000001100 0000011100000000000001100 0000111100000000000001100 0000111000000000000011100 0001111000000000000011100 0001111000000000000011100 0001110000000000000111100 0011110000000000000111000 0011110000000000001110000 0011110000000000001110000 0011110000000000011100000 0011110000000000111000000 0011111000000001100000000 0001111100000011000000000 0000111111111110000000000 0000000000000000000000000 0000000000000000000000000"
Bmp.AddTextData "1 0000000000000000000000 0000000000000000000000 0000000000000000100000 0000000000000000011100000 0000000000000000011100000 0000000000000000011100000 0000000000000000111100000 0000000000000000111000000 0000000000000000111000000 0000000000000001111000000 0000000000000001110000000 0000000000000011110000000 0000000000000011100000000 0000000000000011100000000 0000000000000111100000000 0000000000000111000000000 0000000000001111000000000 0000000000001111000000000 0000000000001110000000000 0000000000011110000000000 0000000000011110000000000 0000000000011100000000000 0000000000111100000000000 0000000000111100000000000 0000000000000000000000000"
Bmp.AddTextData "2 0000000000000000000000 0000000000000000000000 0000000000001111111110 0000000000000111000001110 0000000000011100000000110 0000000000111000000000110 0000000001110000000000110 0000000011100000000000110 0000000111100000000000110 0000000111000000000001110 0000001111000000000001110 0000001111000000000011100 0000001111111000000111000 0000000011000000001110000 0000000000000000011100000 0000000000000001110000000 0000000000000011000000000 0000000000001110000000000 0000000000111000000000000 0000000111100000000000000 0000011100000000000000000 0011110000000000000000000 0111111111100111111111000 0111111111111111100000000 0000000000000000000000000"
Bmp.AddTextData "3 0000000000000111111110 0000000000111100000010 0000000011111000000010 0000000000111100000000010 0000000001111000000000010 0000000001110000000000110 0000000000000000000000110 0000000000000000000001110 0000000000000000000111100 0000000000000000011110000 0000000000111111110000000 0000000001111111111111100 0000000000000000000111110 0000000000000000000111110 0000000000000000000111100 0000000000000000000111100 0000000000000000001111000 0000000000000000001110000 0000000000000000011100000 0000000000000000111000000 0000000000000001110000000 0110000000000111000000000 0111100000111100000000000 0111111111100000000000000 0000111000000000000000000"
Bmp.AddTextData "4 0000000000000000000000 0000000000000001110000 0000000000001111000000 0000000000001111100000000 0000000000001111000000000 0000000000011110000000000 0000000000111100000000000 0000000001111000001110000 0000000011110000011110000 0000000111100000011110000 0000001110000000011100000 0000001100000000111100000 0000011000000000111000000 0000110000000000111000000 0001100000000001111000000 0011100000000001110000000 0011111111111111111111100 0011111111111111110000000 0000000000000011100000000 0000000000000111100000000 0000000000000111000000000 0000000000000111000000000 0000000000001111000000000 0000000000001110000000000 0000000000000000000000000"
Bmp.AddTextData "5 0000000000000000000000 0000000001110000000110 0000000011111111111110 0000000000011110000000000 0000000000011100000000000 0000000000111100000000000 0000000000111100000000000 0000000001111000000000000 0000000001111000000000000 0000000001110011111111000 0000000011111110000111110 0000000011110000000011110 0000000011000000000001110 0000000000000000000011110 0000000000000000000011110 0000000000000000000011110 0000000000000000000111100 0000000000000000000111000 0000000000000000001110000 0000000000000000011100000 0000000000000000111000000 0000000000000011100000000 0000000000001110000000000 0111111111111000000000000 0011111110000000000000000"
Bmp.AddTextData "6 0000000000000000000000 0000000000000000000100 0000000000000000011100 0000000000000000001111100 0000000000000000011110000 0000000000000001111000000 0000000000000011110000000 0000000000000111000000000 0000000000011110000000000 0000000000111000000000000 0000000001110111111110000 0000000011111100011111100 0000000111000000000111100 0000001110000000000011100 0000011100000000000011100 0000111000000000000011100 0001110000000000000111100 0001110000000000000111000 0011100000000000001111000 0111100000000000001110000 0111100000000000011100000 0111100000000000110000000 0111110000000011100000000 0111111000001110000000000 0011111111111000000000000"
Bmp.AddTextData "7 0000000000000000000000 0001111110000000000000 0001111111111111111000 0000000111111111000000000 0000000000000000000001000 0000000000000000000011000 0000000000000000001110000 0000000000000000011100000 0000000000000000111000000 0000000000000001100000000 0000000000000111000000000 0000000000001110000000000 0000000000011110000000000 0000000000111100000000000 0000000001111000000000000 0000000011110000000000000 0000000111100000000000000 0000000111000000000000000 0000001111000000000000000 0000011110000000000000000 0000111100000000000000000 0001111100000000000000000 0001111000000000000000000 0001110000000000000000000 0001110000000000000000000"
Bmp.AddTextData "8 0000000000000000000000 0000000000000000000000 0000000000011111111100 0000000000011110000011100 0000000000111000000001100 0000000001110000000001100 0000000011110000000001100 0000000111100000000001100 0000000111100000000011100 0000000111100000000111000 0000000111110000001110000 0000000011111111111000000 0000000001111111111110000 0000000111000000001111000 0000011100000000000111100 0000111000000000000111100 0001110000000000000111100 0011110000000000001111000 0111100000000000001111000 0111100000000000011110000 0111100000000000111100000 0111110000000001111000000 0011111000000111100000000 0001111111111100000000000 0000000000000000000000000"
Bmp.AddTextData "9 0000000000001111111100 0000000001111000001100 0000000111000000000100 0000000001100000000000100 0000000111000000000000100 0000001111000000000000100 0000001110000000000000100 0000011100000000000001100 0000111100000000000001100 0000111100000000000011100 0001111100000000000111000 0001111100000000001110000 0001111100000000111100000 0000111110000111111000000 0000011111111100110000000 0000000000000001100000000 0000000000000111000000000 0000000000001110000000000 0000000000011100000000000 0000000001111000000000000 0000000111100000000000000 0000011111000000000000000 0001111100000000000000000 0111110000000000000000000 0111000000000000000000000"


Bmp.SessionName = "VerifyCode"   '验证码用Session名称
Bmp.TextLength = 14   '验证码长度
Bmp.BackColor = "#FFFFFF"   '背景颜色,多个颜色用"," 分隔
Bmp.TextColor = "#FF0000,#FF9900,#33CCFF,#99CC00,#FF99FF,#9933FF,#0099FF"    '文字颜色,多个颜色用"," 分
Bmp.YawpColor = Bmp.TextColor   '噪波点颜色,多个颜色使用"," 分隔
Bmp.YawpCount = 3     '噪波点比例 0-100
Bmp.Write()
%>