您目前尚未登陆,请选择【登陆】或【注册
首页->控件插件->Vb.net验证码控件及Demo源码>>WebControlCaptcha/CaptchaImage.vb>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:Vb.net验证码控件及Demo源码
当前文件:文件类型 VBCaptcha/WebControlCaptcha/CaptchaImage.vb打开代码结构图
普通视图
		            
1Imports System 2Imports System.Drawing 3Imports System.Drawing.Drawing2D 4Imports 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> 16Public Class 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 32Public Enums 68 69Public Properties 219 220 Public 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() 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() 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() 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(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(ByVal rect As Rectangle) As PointF 282 Return RandomPoint(rect.Left, rect.Width, rect.Top, rect.Bottom) 283 End Function 284 285