06-09-2011, 07:22 AM
Hey people ,
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:
This is mine:
Step 1
First, let us make the Function which generates the random string.
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:
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'.
Next, the actual check:
Step 4
Double click the button, we need to call the 'Check' function and insert the textbox its text into the parameter.
NOTE: I placed everything from Step 1 to Step 3 in a 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:
I've uploaded the code to Pastebin
Pastebin Link
Raw Pastebin Link (For C+P)
Please comment & rate!
Enjoy!
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:
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!
Enjoy!