Results 1 to 10 of 10

Thread: Captcha Generator

  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.

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Captcha Generator

    That's cool

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Captcha Generator

    This quick DeCaptcha removes the grid, the background, and all the white flecks:
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Using openDialog As New OpenFileDialog()
                openDialog.Filter = "All Images|*.jpg;*.jpeg;*.gif;*.tif;*.tiff;*.bmp;*.png"
                If openDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then _
                    Me.BackgroundImage = Image.FromFile(openDialog.FileName)
            End Using
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim b As New Bitmap(Me.BackgroundImage)
            Dim bd As Imaging.BitmapData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height), Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppArgb)
            Dim arr(bd.Width * bd.Height - 1) As Integer
            Dim arr2(arr.Length - 1) As Integer
            Runtime.InteropServices.Marshal.Copy(bd.Scan0, arr, 0, arr.Length)
            Runtime.InteropServices.Marshal.Copy(bd.Scan0, arr2, 0, arr.Length)
            For i As Integer = 0 To arr.Length - 1
                If arr(i) <> &HFF000000 Then arr(i) = &HFFFFFFFF
            Next
    
            For x As Integer = 2 To bd.Width - 3
                For y As Integer = 2 To bd.Height - 3
                    Dim n As Integer = 0
                    For dX As Integer = -2 To 2
                        For dY As Integer = -2 To 2
                            If arr2((y + dY) * bd.Width + x + dX) = &HFF000000 Then n += 1
                        Next
                    Next
                    If n >= 8 Then arr(y * bd.Width + x) = &HFF000000
                Next
            Next
            Runtime.InteropServices.Marshal.Copy(arr, 0, bd.Scan0, arr.Length)
            b.UnlockBits(bd)
            Me.BackgroundImage = b
        End Sub
    End Class
    I didn't have time to remove the rest of the things (only 5 minutes ) but I'll give it another try later

  4. #4

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

    Re: Captcha Generator

    lol that is scary, since I've seen lots of captchas that were less distorted than this on the internet. Nice code btw. But I will now have to make it more secure.

  5. #5
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Manchester
    Posts
    266

    Re: Captcha Generator

    Very good. A few suggestions to make it more secure:
    - Have the grid as a set of randomly colored lines, perhaps with a gradient. Not just the blue color.
    - Have some of the characters overlapping
    - Have the individual cells in the grid different widths / heights
    - Put another line in but make it diagonal
    - Have the characters themselves random colors and fonts each time
    - Have one character that is slightly off the image

    Hope this helps, but a great start anyway mate!

    Jord

  6. #6

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

    Re: Captcha Generator

    I will probably redo this whole project when it's school holidays, currently I'm buried under a mountain of work, although It would probably only take a day at the longest.

  7. #7
    Member
    Join Date
    Jun 2011
    Posts
    42

    Re: Captcha Generator

    lol how to run this code? i copied and pasted it on a new form and changes class name to whatever it is, does not work
    While calling functions it is not working
    Last edited by Heiningba; Jul 2nd, 2011 at 11:46 PM.

  8. #8
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Captcha Generator

    Quote Originally Posted by Heiningba View Post
    lol how to run this code? i copied and pasted it on a new form and changes class name to whatever it is, does not work
    While calling functions it is not working
    Create a new project. Add a new PictureBox and a button into your form. And use this code:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.  
    5.         '~~~ Create the object
    6.         Dim myCaptcha As New captcha
    7.  
    8.         '~~~ Generate the captcha image and assign this image to the picturebox
    9.         PictureBox1.Image = myCaptcha.generatecaptcha(5)
    10.  
    11.     End Sub
    12.  
    13.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    14.  
    15.         PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize  '~~~ or, you can drag the picturebox to a bigger size(at design time) that could afford the image generated
    16.         Me.WindowState = FormWindowState.Maximized  '~~~ show the form as maximized
    17.  
    18.     End Sub
    19.  
    20. End Class
    Then add a new class(Project menu --> Add new class).
    And copy-paste the code from the first post of this thread.

    Then run your project.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  9. #9

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

    Re: Captcha Generator

    I have holidays now, So I will be redoing this whole class over a few weeks.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  10. #10
    Member
    Join Date
    Jun 2011
    Posts
    42

    Re: Captcha Generator

    mmm great thanks for the code and thanks for letting me know how to run it too. i was wondering where that captcha image would come....did not think about picture box T.T

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