What's new

Creating a Captcha Validation in Visual Basic

  • Thread starter NeverFamousGuy
  • Start date
  • Views 942
NeverFamousGuy

NeverFamousGuy

Issa Knife
Messages
168
Reaction score
42
Points
125
Sin$
7
Concept

The basic thing we are goin' to do is the following steps:

1) Generating Random characters.
2)Printing them in a picture box.
3) Making them scratched.

(1)Generating Random Characters

NOTE:Make sure that you write "Randomize" Command in the form_load event.
Code:
    dim fck1,fck2,fck3,fck4 as integer

   

    fck1 = Int((91 - 65 + 1) * Rnd + 65)

    fck3 = Int((91 - 65 + 1) * Rnd + 65)

    fck2 = Int((123 - 97 + 1) * Rnd + 97)

    fck4 = Int((123 - 97 + 1) * Rnd + 97)

    gen1 = Chr(fck1) & Chr(fck2) & Chr(fck3) & Chr(fck4)

    generated1 = Chr$(fck1) & " " & Chr(fck2) & " " & Chr(fck3) & " " & Chr(fck4)

Explanation : Fck 1,2,3 and 4 are variables for storing ascii code of the character which is generated randomly.

65 to 91 is the ascii code of Capital A-Z
97 to 123 is ascii code of Small(lower case) a-z

so, gen1 will contain a FOUR letter word. This we are using for checking purposes

generated1 is the text to be shown in the picture box...(its used because spaces look nice)

(2)Printing in Picture Box
Code:
PictureBox1.print generated1

(3)Making them scratched(using lines)
Code:
For i = 1 To 60
X1 = (3000 * Rnd) + 1
X2 = (3000 * Rnd) + 1
x3 = (3000 * Rnd) + 1
x4 = (3000 * Rnd) + 1
Y1 = (3000 * Rnd) + 1
Y2 = (3000 * Rnd) + 1
y3 = (3000 * Rnd) + 1
y4 = (3000 * Rnd) + 1
z1 = (3000 * Rnd) + 1
z2 = (3000 * Rnd) + 1
z3 = (3000 * Rnd) + 1
z4 = (3000 * Rnd) + 1
a1 = (3000 * Rnd) + 1
a2 = (3000 * Rnd) + 1
a3 = (3000 * Rnd) + 1
a4 = (3000 * Rnd) + 1
b1 = (3000 * Rnd) + 1
b3 = (3000 * Rnd) + 1
b2 = (3000 * Rnd) + 1
b4 = (3000 * Rnd) + 1
PictureBox1.Line (X1, X2)-(x3, x4), vbWhite
PictureBox1.Line (Y1, Y2)-(y3, y4), vbWhite
PictureBox1.Line (z1, z2)-(z3, z4), vbWhite
PictureBox1.Line (a1, a2)-(a3, a4), vbWhite
PictureBox1.Line (b1, b2)-(b3, b4), vbWhite
Next i

Explanation : In the variables a1234, b1234, z1234,x1234,y1234 we are genrating a number between 1 to 3000(Because 3000x3000 is dimension of picture box). These are the req. coordinates.

Now we just simply print the lines in the the picture box with these coordinates.

NOTE : To increase the intensity of lines, just increase the upper bound of loop.

The result printed will be Four letters with some(or more) scratches, hardly readable.
Code:
If Text1.Text = gen1 Then
MsgBox ("correct")
///You can also do 
///Form2.show(); or sql login blah blah blah 
///Pretty much call your following command following the case that it was correct...
Else
MsgBox ("wrong")
///This is where you can put me.Close or whatever if its wrong
End If
 
Top Bottom
Login
Register