ASP生成BMP验证码图片

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
''''''''''''''''''''''''''''''''''''''''''''''''''''''
'ASP生成BMP验证码图片 
'石卓林(精彩)
'Site URL:Http://www.camxam.com
'EMail:camxam@163.com
'QQ:383193853
'MSN:camxam@163.com
'2006年3月9日 星期四 
'使用方法:在需要显示的页面嵌入  <img Src="Bmp.asp" />, 在验证码检验中使用 Session(SessionName属性值) 作为验证码参考值
'属性:
'   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
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Class CAMXAM_COM_RndCodeBmp
    Private TextDataLib,TextData,BmpData,BmpHead,BmpWidth,BmpHeight,BmpSize,TextDataLength,SessionString
    Public SessionName,TextLength,BackColor,TextColor,YawpColor,YawpCount
    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 GetColorString(ColorString)
        Dim ColorArray,CALength
            ColorArray = Split(ColorString,",")
            CALength = UBound(ColorArray)
            GetColorString = 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 = 0
        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) '类型"BM"
        BmpHead = BmpHead & Getbinary(BmpSize+54) '文件的大小(字节)
        BmpHead = BmpHead & Getbinary(0) '保留必须为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) '用字节数表示的位图数据的大小。该数必须是4的倍数
        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) = GetColorString(BackColor) & " " & GetColorString(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(ColorString)
        GetColor = ChrB("&H" & Mid(ColorString,6,2)) & ChrB("&H" & Mid(ColorString,4,2)) & ChrB("&H" & Mid(ColorString,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 * 3
        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(GetColorString(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
End Class
Dim Bmp
Set Bmp = New CAMXAM_COM_RndCodeBmp
    Bmp.SetTextInfo "8 × 12 字库 字库来源 Wh_BmpText http://vcc5.vicp.net Email: wuyingke5155@163.com Oicq:54883661"  '字库信息
    Bmp.AddTextData "0 00000000 00000000 00111000 01000100 01000100 01000100 01000100 01000100 01000100 00111000 00000000 00000000"
    Bmp.AddTextData "1 00000000 00000000 00110000 01010000 00010000 00010000 00010000 00010000 00010000 01111100 00000000 00000000"
    Bmp.AddTextData "2 00000000 00000000 01111000 00000100 00000100 00001000 00010000 00100000 01000000 01111100 00000000 00000000"
    Bmp.AddTextData "3 00000000 00000000 01111000 00000100 00000100 00111000 00000100 00000100 00000100 01111000 00000000 00000000"
    Bmp.AddTextData "4 00000000 00000000 00001000 00011000 00101000 00101000 01001000 01111100 00001000 00001000 00000000 00000000"
    Bmp.AddTextData "5 00000000 00000000 01111000 01000000 01000000 01110000 00001000 00001000 00001000 01110000 00000000 00000000"
    Bmp.AddTextData "6 00000000 00000000 00111100 01000000 01000000 01011000 01100100 01000100 01000100 00111000 00000000 00000000"
    Bmp.AddTextData "7 00000000 00000000 01111100 00000100 00001000 00010000 00100000 00100000 01000000 01000000 00000000 00000000"
    Bmp.AddTextData "8 00000000 00000000 00111000 01000100 01000100 00111000 01001100 01000100 01000100 00111000 00000000 00000000"
    Bmp.AddTextData "9 00000000 00000000 00111000 01000100 01000100 01001100 00110100 00000100 00000100 01111000 00000000 00000000"
    Bmp.AddTextData "A 00000000 00000000 00010000 00101000 00101000 00101000 01000100 01111100 01000100 10000010 00000000 00000000"
    Bmp.AddTextData "B 00000000 00000000 01111000 01000100 01000100 01111000 01001000 01000100 01000100 01111000 00000000 00000000"
    Bmp.AddTextData "C 00000000 00000000 00011100 00100000 01000000 01000000 01000000 01000000 00100000 00011100 00000000 00000000"
    Bmp.AddTextData "D 00000000 00000000 01110000 01001000 01000100 01000100 01000100 01000100 01001000 01110000 00000000 00000000"
    Bmp.AddTextData "E 00000000 00000000 01111100 01000000 01000000 01111000 01000000 01000000 01000000 01111100 00000000 00000000"
    Bmp.AddTextData "F 00000000 00000000 01111100 01000000 01000000 01111000 01000000 01000000 01000000 01000000 00000000 00000000"
    Bmp.AddTextData "G 00000000 00000000 00011100 00100000 01000000 01000000 01000100 01000100 00100100 00011100 00000000 00000000"
    Bmp.AddTextData "H 00000000 00000000 01000100 01000100 01000100 01111100 01000100 01000100 01000100 01000100 00000000 00000000"
    Bmp.AddTextData "I 00000000 00000000 01111100 00010000 00010000 00010000 00010000 00010000 00010000 01111100 00000000 00000000"
    Bmp.AddTextData "J 00000000 00000000 00111000 00001000 00001000 00001000 00001000 00001000 00001000 01110000 00000000 00000000"
    Bmp.AddTextData "K 00000000 00000000 01000100 01001000 01010000 01100000 01100000 01010000 01001000 01000100 00000000 00000000"
    Bmp.AddTextData "L 00000000 00000000 01000000 01000000 01000000 01000000 01000000 01000000 01000000 01111100 00000000 00000000"
    Bmp.AddTextData "M 00000000 00000000 10000100 10000100 11001100 11001100 10110100 10110100 10000100 10000100 00000000 00000000"
    Bmp.AddTextData "N 00000000 00000000 01000100 01100100 01100100 01010100 01010100 01001100 01001100 01000100 00000000 00000000"
    Bmp.AddTextData "O 00000000 00000000 00010000 00101000 01000100 01000100 01000100 01000100 00101000 00010000 00000000 00000000"
    Bmp.AddTextData "P 00000000 00000000 01111000 01000100 01000100 01001000 01110000 01000000 01000000 01000000 00000000 00000000"
    Bmp.AddTextData "Q 00000000 00000000 00010000 00101000 01000100 01000100 01000100 01000100 00101000 00011000 00000100 00000000"
    Bmp.AddTextData "R 00000000 00000000 01111000 01000100 01000100 01000100 01111000 01001000 01000100 01000100 00000000 00000000"
    Bmp.AddTextData "S 00000000 00000000 00111100 01000000 01000000 00110000 00001000 00000100 00000100 01111000 00000000 00000000"
    Bmp.AddTextData "T 00000000 00000000 11111110 00010000 00010000 00010000 00010000 00010000 00010000 00010000 00000000 00000000"
    Bmp.AddTextData "U 00000000 00000000 01000100 01000100 01000100 01000100 01000100 01000100 01000100 00111000 00000000 00000000"
    Bmp.AddTextData "V 00000000 00000000 10000010 01000100 01000100 01000100 00101000 00101000 00101000 00010000 00000000 00000000"
    Bmp.AddTextData "W 00000000 00000000 10000010 10000010 10010010 10010010 01101100 01101100 01000100 01000100 00000000 00000000"
    Bmp.AddTextData "X 00000000 00000000 01000100 00101000 00101000 00010000 00010000 00101000 01000100 01000100 00000000 00000000"
    Bmp.AddTextData "Y 00000000 00000000 01000100 01000100 00101000 00101000 00010000 00010000 00010000 00010000 00000000 00000000"
    Bmp.AddTextData "Z 00000000 00000000 01111100 00000100 00001000 00010000 00010000 00100000 01000000 01111100 00000000 00000000"
    Bmp.SessionName = "code"    '验证码用Session名称
    Bmp.TextLength = 6  '验证码长度
    Bmp.BackColor = "#FFFFFF"   '背景颜色,多个颜色用"," 分隔
    Bmp.TextColor = "#FF0000,#00FF00,#0000FF"   '文字颜色,多个颜色用"," 分
    Bmp.YawpColor = "#000000"   '噪波点颜色,多个颜色使用"," 分隔
    Bmp.YawpCount = 10  '噪波点比例 0-100
    Bmp.Write()
%>

One Response to“ASP生成BMP验证码图片”

  1. Anonymous
    2017年9月6日 at pm6:16 #

    3

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

Proudly powered by WordPress   Premium Style Theme by www.gopiplus.com