Ethernal_Five

[VB.NET] Captcha Generator

Feb 23rd, 2012
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.09 KB | None | 0 0
  1. Imports System.Text
  2. Imports System.Drawing
  3. Imports System.Windows.Forms
  4. Imports System.Drawing.Drawing2D
  5.  
  6. Class CaptchaBox
  7.     Inherits PictureBox
  8. #Region "Properties"
  9.     Private _textcolor As Color = Color.Black
  10.     Public Property TextColor() As Color
  11.         Get
  12.             Return _textcolor
  13.         End Get
  14.         Set(value As Color)
  15.             _textcolor = value
  16.             Invalidate()
  17.         End Set
  18.     End Property
  19.  
  20.     Private _font As New Font("Segoe UI", 20)
  21.     Public Overrides Property Font() As Font
  22.         Get
  23.             Return _font
  24.         End Get
  25.         Set(value As Font)
  26.             _font = value
  27.             Invalidate()
  28.         End Set
  29.     End Property
  30.  
  31.     Private _texttouse As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz!#$%^&*()1234567890"
  32.     Public Property RandomCharacters() As String
  33.         Get
  34.             Return _texttouse
  35.         End Get
  36.         Set(value As String)
  37.             _texttouse = value
  38.             Invalidate()
  39.         End Set
  40.     End Property
  41.  
  42.     Private _captchalength As Integer = 8
  43.     Public Property CaptchaTextLength() As Integer
  44.         Get
  45.             Return _captchalength
  46.         End Get
  47.         Set(value As Integer)
  48.             _captchalength = value
  49.             Invalidate()
  50.         End Set
  51.     End Property
  52.  
  53.     Private _numberoflines As Integer = 50
  54.     Public Property NumberOfLines() As Integer
  55.         Get
  56.             Return _numberoflines
  57.         End Get
  58.         Set(value As Integer)
  59.             _numberoflines = value
  60.             Invalidate()
  61.         End Set
  62.     End Property
  63. #End Region
  64.  
  65. #Region "Events"
  66.     Public Sub New()
  67.         SetStyle(ControlStyles.AllPaintingInWmPaint, True)
  68.         SetStyle(ControlStyles.ResizeRedraw, True)
  69.         SetStyle(ControlStyles.UserPaint, True)
  70.         SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
  71.         SetStyle(ControlStyles.SupportsTransparentBackColor, True)
  72.     End Sub
  73.  
  74.     Protected Overrides Sub CreateHandle()
  75.         MyBase.CreateHandle()
  76.         Me.Size = New Size(241, 69)
  77.         Me.BorderStyle = BorderStyle.FixedSingle
  78.     End Sub
  79.  
  80.     Public CaptchaText As String = String.Empty
  81.     Private rnd As New Random()
  82.     Protected Overrides Sub OnPaint(e As PaintEventArgs)
  83.         Dim B As New Bitmap(Width, Height)
  84.         Dim G As Graphics = Graphics.FromImage(B)
  85.  
  86.         Dim BoxSize As New Rectangle(0, 0, Me.Width, Me.Height)
  87.         Dim txtcolor As Brush = New SolidBrush(Color.FromArgb(rnd.[Next](160, 255), _textcolor))
  88.         G.SmoothingMode = SmoothingMode.HighQuality
  89.  
  90.         G.Clear(BackColor)
  91.  
  92.         For I As Integer = 0 To _numberoflines - 1
  93.             G.DrawLine(New Pen(Color.FromArgb(rnd.[Next](128, 255), CreateRandomColor())), rnd.[Next](0, BoxSize.Width), rnd.[Next](0, BoxSize.Height), rnd.[Next](BoxSize.Width), rnd.[Next](BoxSize.Height))
  94.         Next
  95.  
  96.         Dim SFormat As New StringFormat()
  97.         SFormat.Alignment = StringAlignment.Center
  98.         SFormat.LineAlignment = StringAlignment.Center
  99.  
  100.         G.RotateTransform(rnd.[Next](-7, 7), MatrixOrder.Append)
  101.  
  102.         Dim randomstr As String = CreateRandomString()
  103.         G.DrawString(randomstr, Font, txtcolor, BoxSize, SFormat)
  104.         CaptchaText = randomstr
  105.  
  106.         e.Graphics.DrawImage(B, 0, 0)
  107.         G.Dispose()
  108.         B.Dispose()
  109.     End Sub
  110. #End Region
  111.  
  112. #Region "Create Randoms"
  113.     Private Function CreateRandomColor() As Color
  114.         Return Color.FromArgb(100, rnd.[Next](255), rnd.[Next](255), rnd.[Next](255))
  115.     End Function
  116.  
  117.     Private Function CreateRandomString() As String
  118.         Dim buffer As Char() = New Char(_captchalength - 1) {}
  119.         For i As Integer = 0 To _captchalength - 1
  120.             If _texttouse = String.Empty Then
  121.                 Dim newtext As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz1234567890"
  122.                 buffer(i) = newtext(rnd.[Next](newtext.Length))
  123.             Else
  124.                 buffer(i) = _texttouse(rnd.[Next](_texttouse.Length))
  125.             End If
  126.         Next
  127.         Return New String(buffer)
  128.     End Function
  129. #End Region
  130. End Class
Advertisement
Add Comment
Please, Sign In to add comment