1
Imports System
2
Imports System.Drawing
3
Imports System.Drawing.Drawing2D
4
Imports System.Drawing.Imaging
5
6
/**/''' <summary>
7
''' CAPTCHA image generation class
8
''' </summary>
9
''' <remarks>
10
''' Adapted from the excellent code at
11
''' http://www.codeproject.com/aspnet/CaptchaImage.asp
12
''' 该源码下载自www.51aspx.com(51aspx.com)
13
''' Jeff Atwood
14
''' http://www.codinghorror.com/
15
''' </remarks>
16
Public Class CaptchaImageClass CaptchaImage
17
18
Private _height As Integer
19
Private _width As Integer
20
Private _rand As Random
21
Private _generatedAt As DateTime
22
Private _randomText As String
23
Private _randomTextLength As Integer
24
Private _randomTextChars As String
25
Private _fontFamilyName As String
26
Private _fontWarp As FontWarpFactor
27
Private _backgroundNoise As BackgroundNoiseLevel
28
Private _lineNoise As LineNoiseLevel
29
Private _guid As String
30
Private _fontWhitelist As String
31
32
Public Enums#Region " Public Enums"
33
34
/**/''' <summary>
35
''' Amount of random font warping to apply to rendered text
36
''' </summary>
37
Public Enum FontWarpFactorEnum FontWarpFactor
38
None
39
Low
40
Medium
41
High
42
Extreme
43
End Enum
44
45
/**/''' <summary>
46
''' Amount of background noise to add to rendered image
47
''' </summary>
48
Public Enum BackgroundNoiseLevelEnum BackgroundNoiseLevel
49
None
50
Low
51
Medium
52
High
53
Extreme
54
End Enum
55
56
/**/''' <summary>
57
''' Amount of curved line noise to add to rendered image
58
''' </summary>
59
Public Enum LineNoiseLevelEnum LineNoiseLevel
60
None
61
Low
62
Medium
63
High
64
Extreme
65
End Enum
66
67
#End Region
68
69
Public Properties#Region " Public Properties"
70
71
/**/''' <summary>
72
''' Returns a GUID that uniquely identifies this Captcha
73
''' </summary>
74
Public ReadOnly Property UniqueId()Property UniqueId() As String
75
Get
76
Return _guid
77
End Get
78
End Property
79
80
/**/''' <summary>
81
''' Returns the date and time this image was last rendered
82
''' </summary>
83
Public ReadOnly Property RenderedAt()Property RenderedAt() As DateTime
84
Get
85
Return _generatedAt
86
End Get
87
End Property
88
89
/**/''' <summary>
90
''' Font family to use when drawing the Captcha text. If no font is provided, a random font will be chosen from the font whitelist for each character.
91
''' </summary>
92
Public Property Font()Property Font() As String
93
Get
94
Return _fontFamilyName
95
End Get
96
Set(ByVal Value As String)
97
Try
98
Dim font1 As Font = New Font(Value, 12.0!)
99
_fontFamilyName = Value
100
font1.Dispose()
101
Catch ex As Exception
102
_fontFamilyName = Drawing.FontFamily.GenericSerif.Name
103
End Try
104
End Set
105
End Property
106
107
/**/''' <summary>
108
''' Amount of random warping to apply to the Captcha text.
109
''' </summary>
110
Public Property FontWarp()Property FontWarp() As FontWarpFactor
111
Get
112
Return _fontWarp
113
End Get
114
Set(ByVal Value As FontWarpFactor)
115
_fontWarp = Value
116
End Set
117
End Property
118
119
/**/''' <summary>
120
''' Amount of background noise to apply to the Captcha image.
121
''' </summary>
122
Public Property BackgroundNoise()Property BackgroundNoise() As BackgroundNoiseLevel
123
Get
124
Return _backgroundNoise
125
End Get
126
Set(ByVal Value As BackgroundNoiseLevel)
127
_backgroundNoise = Value
128
End Set
129
End Property
130
131
Public Property LineNoise()Property LineNoise() As LineNoiseLevel
132
Get
133
Return _lineNoise
134
End Get
135
Set(ByVal value As LineNoiseLevel)
136
_lineNoise = value
137
End Set
138
End Property
139
140
/**/''' <summary>
141
''' A string of valid characters to use in the Captcha text.
142
''' A random character will be selected from this string for each character.
143
''' </summary>
144
Public Property TextChars()Property TextChars() As String
145
Get
146
Return _randomTextChars
147
End Get
148
Set(ByVal Value As String)
149
_randomTextChars = Value
150
_randomText = GenerateRandomText()
151
End Set
152
End Property
153
154
/**/''' <summary>
155
''' Number of characters to use in the Captcha text.
156
''' </summary>
157
Public Property TextLength()Property TextLength() As Integer
158
Get
159
Return _randomTextLength
160
End Get
161
Set(ByVal Value As Integer)
162
_randomTextLength = Value
163
_randomText = GenerateRandomText()
164
End Set
165
End Property
166
167
/**/''' <summary>
168
''' Returns the randomly generated Captcha text.
169
''' </summary>
170
Public ReadOnly Property [()Property [Text]() As String
171
Get
172
Return _randomText
173
End Get
174
End Property
175
176
/**/''' <summary>
177
''' Width of Captcha image to generate, in pixels
178
''' </summary>
179
Public Property Width()Property Width() As Integer
180
Get
181
Return _width
182
End Get
183
Set(ByVal Value As Integer)
184
If (Value <= 60) Then
185
Throw New ArgumentOutOfRangeException("width", Value, "width must be greater than 60.")
186
End If
187
_width = Value
188
End Set
189
End Property
190
191
/**/''' <summary>
192
''' Height of Captcha image to generate, in pixels
193
''' </summary>
194
Public Property Height()Property Height() As Integer
195
Get
196
Return _height
197
End Get
198
Set(ByVal Value As Integer)
199
If Value <= 30 Then
200
Throw New ArgumentOutOfRangeException("height", Value, "height must be greater than 30.")
201
End If
202
_height = Value
203
End Set
204
End Property
205
206
/**/''' <summary>
207
''' A semicolon-delimited list of valid fonts to use when no font is provided.
208
''' </summary>
209
Public Property FontWhitelist()Property FontWhitelist() As String
210
Get
211
Return _fontWhitelist
212
End Get
213
Set(ByVal value As String)
214
_fontWhitelist = value
215
End Set
216
End Property
217
218
#End Region
219
220
Public Sub New()Sub New()
221
_rand = New Random
222
_fontWarp = FontWarpFactor.Low
223
_backgroundNoise = BackgroundNoiseLevel.Low
224
_lineNoise = LineNoiseLevel.None
225
_width = 180
226
_height = 50
227
_randomTextLength = 5
228
_randomTextChars = "ACDEFGHJKLNPQRTUVXYZ2346789"
229
_fontFamilyName = ""
230
' -- a list of known good fonts in on both Windows XP and Windows Server 2003
231
_fontWhitelist = _
232
"arial;arial black;comic sans ms;courier new;estrangelo edessa;franklin gothic medium;" & _
233
"georgia;lucida console;lucida sans unicode;mangal;microsoft sans serif;palatino linotype;" & _
234
"sylfaen;tahoma;times new roman;trebuchet ms;verdana"
235
_randomText = GenerateRandomText()
236
_generatedAt = DateTime.Now
237
_guid = Guid.NewGuid.ToString()
238
End Sub
239
240
/**/''' <summary>
241
''' Forces a new Captcha image to be generated using current property value settings.
242
''' </summary>
243
Public Function RenderImage()Function RenderImage() As Bitmap
244
Return GenerateImagePrivate()
245
End Function
246
247
/**/''' <summary>
248
''' Returns a random font family from the font whitelist
249
''' </summary>
250
Private Function RandomFontFamily()Function RandomFontFamily() As String
251
Static ff() As String
252
'-- small optimization so we don't have to split for each char
253
If ff Is Nothing Then
254
ff = _fontWhitelist.Split(";"c)
255
End If
256
Return ff(_rand.Next(0, ff.Length))
257
End Function
258
259
/**/''' <summary>
260
''' generate random text for the CAPTCHA
261
''' </summary>
262
Private Function GenerateRandomText()Function GenerateRandomText() As String
263
Dim sb As New System.Text.StringBuilder(_randomTextLength)
264
Dim maxLength As Integer = _randomTextChars.Length
265
For n As Integer = 0 To _randomTextLength - 1
266
sb.Append(_randomTextChars.Substring(_rand.Next(maxLength), 1))
267
Next
268
Return sb.ToString
269
End Function
270
271
/**/''' <summary>
272
''' Returns a random point within the specified x and y ranges
273
''' </summary>
274
Private Function RandomPoint()Function RandomPoint(ByVal xmin As Integer, ByVal xmax As Integer, ByRef ymin As Integer, ByRef ymax As Integer) As PointF
275
Return New PointF(_rand.Next(xmin, xmax), _rand.Next(ymin, ymax))
276
End Function
277
278
/**/''' <summary>
279
''' Returns a random point within the specified rectangle
280
''' </summary>
281
Private Function RandomPoint()Function RandomPoint(ByVal rect As Rectangle) As PointF
282
Return RandomPoint(rect.Left, rect.Width, rect.Top, rect.Bottom)
283
End Function
284
285