Support Forums
[VB.Net] Simple captcha class [Commented][Noob/Friendly] - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19)
+---- Thread: [VB.Net] Simple captcha class [Commented][Noob/Friendly] (/showthread.php?tid=19444)

Pages: 1 2


[VB.Net] Simple captcha class [Commented][Noob/Friendly] - The-One - 06-09-2011

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!


RE: [VB.Net] Simple captcha class [Commented][Noob/Friendly] - davidmarker - 06-09-2011

Thanks! I'll have to implant this in some of my future projects.


RE: [VB.Net] Simple captcha class [Commented][Noob/Friendly] - xMan - 06-09-2011

wow this is one in depth tutorial. well done with it!


RE: [VB.Net] Simple captcha class [Commented][Noob/Friendly] - The-One - 06-09-2011

(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!


RE: [VB.Net] Simple captcha class [Commented][Noob/Friendly] - -Dreams - 06-14-2011

Kinda dumb in my book, but whatever.


RE: [VB.Net] Simple captcha class [Commented][Noob/Friendly] - The-One - 06-19-2011

(06-14-2011, 02:21 AM)-Dreams Wrote: Kinda dumb in my book, but whatever.

How you mean dumb?


RE: [VB.Net] Simple captcha class [Commented][Noob/Friendly] - RiChZ - 06-19-2011

thank. going to use this on my next project.


RE: [VB.Net] Simple captcha class [Commented][Noob/Friendly] - Coding Support - 06-19-2011

Good, well detailed tutorial. A lot of work for a captcha class. Ill try this out soon.


RE: [VB.Net] Simple captcha class [Commented][Noob/Friendly] - Bigandrewgold - 06-24-2011

good basic captcha. But i just hope that some noob does'nt use this to actually try to protect something


RE: [VB.Net] Simple captcha class [Commented][Noob/Friendly] - Black Ghost - 06-29-2011

This will be useful for a booter I am making.

Thanks!