Thread Rating:
  • 2 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[VB.Net] Simple captcha class [Commented][Noob/Friendly]
#1
Hey people Oui,

This is my first contribution to this forum.

Today we are going to make our own captcha class, meaning we are going to draw and show it ourselves. Why? Because its shorter and probably easier for new programmers.


I assume you have some knowledge of Visual Basic.NET so I won't spoon feed you guys today.

Create a new windows form project and add the following controls:
  • [*] One TextBox
    [*] One PictureBox
    [*] One Button

This is mine:
[Image: jM3mu.png]

Step 1
First, let us make the Function which generates the random string.

Code:
Private Shared Function RandomString(ByVal size As Integer) As String
            Dim random As New Random() ' Define a new random class, we can't do this inside the code, since it will then return only one letter.
            RandomString = Nothing ' Set the string to Empty/Nothing
            For i = 0 To size - 1 ' From 0 to size - 1
                RandomString += Convert.ToChar(Convert.ToInt32((25 * random.NextDouble() + 65))) ' Generate, convert and add.
            Next
            Return RandomString ' Return the string.
        End Function

I fully commented everything so you know what piece of code does what.

Step 2
Okay, cool, we get l33t strings. But how we get them into a image?

Simple:

Code:
Public Shared Sub RandomImage(ByVal p As PictureBox, ByVal c As Color, Optional ByVal string_size As Integer = 6, Optional ByVal letter_size As Integer = 30)
            p.Refresh() ' Remove (if ít's there) the old captcha.
            Dim g As Graphics = p.CreateGraphics ' Assign our Graphics class to the picturebox.
            s = RandomString(string_size) 'Save and generate the new captcha
            g.DrawString(s, New Font(Form1.Font.OriginalFontName, letter_size, FontStyle.Regular), New SolidBrush(c), 0, 0, New StringFormat) ' Draw the string using Graphics.
        End Sub

As you notice, I'm trying to declare as less as I can.

Step 3
Now we are going to check if the picture matches the textbox.

First we need to store the string into the memory, (as you notice a error) so we need to declare 's'.

Code:
Private Shared s As String = Nothing ' Save it to check it later.

Next, the actual check:

Code:
Public Shared Function Check(ByVal str As String) As Boolean
            If str.ToUpper() = s Then Return True Else Return False ' If the stored string matches the parameter(textbox) its string, return true, else return false.
            ' String is generated in Uppercase, so convert the parameter(textbox) to uppercase.
        End Function

Step 4

Double click the button, we need to call the 'Check' function and insert the textbox its text into the parameter.

Code:
If ManCaptcha.Check(TextBox1.Text) Then ' Check if true.
            MsgBox("Passed!") ' Show a confirmation.
            ' Parameters: <picturebox to store picture>, <color of captcha>, <size of captcha string>, <size of each letter>  
            ManCaptcha.RandomImage(PictureBox1, Color.Red, 6, 30) ' Create a new one.
            TextBox1.Clear() ' Clear the textbox.
        Else
            MsgBox("Retry!") ' Show a error.
        End If

NOTE: I placed everything from Step 1 to Step 3 in a STRUCTURE

Code:
Structure ManCaptcha
End Structure

You can skip it, but I like to have it organized.

Step 5

Now, to create the image and draw it, you can use this:

Code:
' Parameters: <picturebox to store picture>, <color of captcha>, <size of captcha string>, <size of each letter>  
            ManCaptcha.RandomImage(PictureBox1, Color.Red, 6, 30) ' Create a new one.


I've uploaded the code to Pastebin

Pastebin Link
Raw Pastebin Link (For C+P)


Please comment & rate! Cool

Enjoy!
[Image: iuyle.png]
Reply
#2
Thanks! I'll have to implant this in some of my future projects.
Reply
#3
wow this is one in depth tutorial. well done with it!
Reply
#4
(06-09-2011, 07:49 AM)xMan Wrote: wow this is one in depth tutorial. well done with it!

Thanks man, appreciate it.

(06-09-2011, 07:48 AM)davidmarker Wrote: Thanks! I'll have to implant this in some of my future projects.

Thanks, good luck with your projects!
[Image: iuyle.png]
Reply
#5
Kinda dumb in my book, but whatever.
Reply
#6
(06-14-2011, 02:21 AM)-Dreams Wrote: Kinda dumb in my book, but whatever.

How you mean dumb?
[Image: iuyle.png]
Reply
#7
thank. going to use this on my next project.
Reply
#8
Good, well detailed tutorial. A lot of work for a captcha class. Ill try this out soon.
[Image: iddyEs.png]
Reply
#9
good basic captcha. But i just hope that some noob does'nt use this to actually try to protect something
Reply
#10
This will be useful for a booter I am making.

Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to display google captcha picture on form and add to textbox hit ok to bypass polas 0 1,265 09-14-2012, 01:59 PM
Last Post: polas
  [TuToRiAl] How to make Offline (No Internet) Captcha (Human Confirmation) [TuToRiAl] Resistance 24 12,527 08-30-2012, 10:14 PM
Last Post: Resistance
  [VB.Net] MySQL connection class [Register/List/Login] The-One 21 11,541 07-20-2011, 09:33 AM
Last Post: PURP
  [Vb.net] Closing Event Example Ok or Cancel [SRC] [Simple] ★ASI_F★™ 9 2,313 07-08-2011, 09:37 PM
Last Post: AceInfinity
  [VB.NET] Graphics Viewer [SRC] [Simple] ★ASI_F★™ 0 948 07-08-2011, 07:58 PM
Last Post: ★ASI_F★™

Forum Jump:


Users browsing this thread: 1 Guest(s)