Results 1 to 10 of 10

Thread: Captcha Generator

Threaded View

  1. #1

    Thread Starter
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Captcha Generator

    I was bored and I made a captcha generator, but I have no use for it so I'm posting it here. I don't know what the standards are for captcha's are but I think mine does a reasonable job.





    vb.net Code:
    1. Imports System.Drawing
    2. Imports System.Drawing.Drawing2D
    3. Public Class CAPTCHA
    4.     Dim cap As String
    5.     Public ReadOnly Property CaptchaString As String
    6.         Get
    7.             Return cap
    8.         End Get
    9.     End Property
    10.  
    11.     Function GenerateCaptcha(ByVal NumberOfCharacters As Integer) As Bitmap
    12.         Dim R As New Random
    13.         Dim VerticalLineSpaceing As Integer = R.Next(5, 10) ' The space between each horizontal line
    14.         Dim HorisontalLineSpaceing As Integer = R.Next(5, 10) ' The space between each Vertical line
    15.  
    16.  
    17.  
    18.         Dim CWidth As Integer = (NumberOfCharacters * 120) 'Generating the width
    19.         Dim CHeight As Integer = 180 ' the height
    20.         Dim CAPTCHA As New Bitmap(CWidth, CHeight)
    21.         Dim allowedCharacters() As Char = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM123456789".ToCharArray 'Guess
    22.         Dim str(NumberOfCharacters - 1) As Char ' The String to turn into a captcha
    23.         For i = 0 To NumberOfCharacters - 1
    24.             str(i) = allowedCharacters(R.Next(0, 61)) ' Generating random characters
    25.         Next
    26.  
    27.         Using g As Graphics = Graphics.FromImage(CAPTCHA)
    28.             Dim gradient As New Drawing2D.LinearGradientBrush(New Point(0, CInt(CHeight / 2)), New Point(CWidth, CInt(CHeight / 2)), Drawing.Color.FromArgb(R.Next(&HFF7D7D7D, &HFFFFFFFF)), Drawing.Color.FromArgb(R.Next(&HFF7D7D7D, &HFFFFFFFF)))
    29.             'ABOVE: the gradient brush for the background
    30.             g.FillRectangle(gradient, New Rectangle(0, 0, CWidth, CHeight))
    31.             Dim plist As New List(Of Point) ' the list of points the curve goes through
    32.  
    33.             For i = 0 To str.Length - 1
    34.                 Dim FHeight As Integer = R.Next(60, 100) 'Font height in EM
    35.                 Dim Font As New Font("Arial", FHeight)
    36.                 Dim Y As Integer = R.Next(0, (CHeight - FHeight) - 40) 'Generating the Y value of a char: will be between the top  and (bottom - 40) to prevent half characters
    37.                 Dim X As Integer = CInt((((i * CWidth) - 10) / NumberOfCharacters))  'Some formula that made sense At the time that I typed it to generate the X value
    38.                 Dim p As New Point(X, Y)
    39.  
    40.                 g.DrawString(str(i).ToString, Font, Brushes.Black, p)
    41.  
    42.                 plist.Add(New Point(X, R.Next(CInt((CHeight / 2) - 40), CInt((CHeight / 2) + 40)))) ' add the points to the array
    43.             Next
    44.  
    45.             plist.Add(New Point(CWidth, CInt(CHeight / 2))) 'for some reason it doesn't go to the end so we manually add the last point
    46.          
    47.             Dim ppen As New Pen(Brushes.Black, R.Next(5, 10)) ' the pen used to draw the curve
    48.  
    49.             g.DrawCurve(ppen, plist.ToArray)
    50.  
    51.             Dim pen As New Pen(Brushes.SteelBlue, CSng(R.Next(1, 2))) 'the pen that will draw the horisontal and vertical lines.
    52.  
    53.             For i = 1 To CWidth
    54.                 Dim ptop As New Point(i * VerticalLineSpaceing, 0)
    55.                 Dim pBottom As New Point(i * VerticalLineSpaceing, CHeight)
    56.                 g.DrawLine(pen, ptop, pBottom)
    57.             Next
    58.  
    59.             'ABOVE Drawing the vertical lines
    60.             For i = 1 To CHeight
    61.                 Dim ptop As New Point(0, i * HorisontalLineSpaceing)
    62.                 Dim pBottom As New Point(CWidth, i * HorisontalLineSpaceing)
    63.                 g.DrawLine(pen, ptop, pBottom)
    64.             Next
    65.  
    66.             'ABOVE: drawing the horizontal lines
    67.  
    68.             Dim numnoise As Integer = CInt(CWidth * CHeight / 25) 'calculating the  number of noise for the block. This will generate 1 Noise per 25X25 block of pixels if im correct
    69.             For i = 1 To numnoise / 2
    70.                 Dim X As Integer = R.Next(0, CWidth)
    71.                 Dim Y As Integer = R.Next(0, CHeight)
    72.                 Dim int As Integer = R.Next(1, 2)
    73.                 g.FillEllipse(Brushes.Black, New Rectangle(X, Y, R.Next(2, 5), R.Next(2, 5))) 'Size of the white noise
    74.             Next
    75.             'Above: Drawing the Black noise particles
    76.  
    77.  
    78.             For i = 1 To numnoise / 2
    79.                 Dim X As Integer = R.Next(0, CWidth)
    80.                 Dim Y As Integer = R.Next(0, CHeight)
    81.                 Dim int As Integer = R.Next(1, 2)
    82.  
    83.  
    84.                 g.FillEllipse(Brushes.White, New Rectangle(X, Y, R.Next(2, 5), R.Next(2, 5))) 'Size of the white noise
    85.             Next
    86.             'Above: Drawing the white noise particles
    87.         End Using
    88.  
    89.  
    90.         cap = str
    91.         Return CAPTCHA
    92.     End Function
    93.     Function Check(ByVal captcha As String, Optional ByVal IgnoreCase As Boolean = False) As Boolean
    94.         If IgnoreCase Then
    95.             If captcha.ToLower = CaptchaString.ToLower Then
    96.                 Return True
    97.  
    98.             Else
    99.                 Return False
    100.  
    101.             End If
    102.         Else
    103.             If captcha = CaptchaString Then
    104.                 Return True
    105.  
    106.             Else
    107.                 Return False
    108.  
    109.             End If
    110.         End If
    111.  
    112.     End Function
    113.  
    114. End Class

    I've Tested this up to 1500 characters. For some reason It doesn't want more than that, although i'm not going to fix that for obvious reasons.

    EDIT: oh and yes I do Know recaptcha is better, but it's a nice simple example of creating your own.
    Last edited by BlindSniper; Mar 31st, 2011 at 12:25 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width