Untitled


SUBMITTED BY: Guest

DATE: Oct. 24, 2014, 1:29 a.m.

FORMAT: Text only

SIZE: 4.5 kB

HITS: 8484

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

comments powered by Disqus