[Tutorial] Home made Captcha w/ user input 07-10-2013, 11:14 AM
#1
Captcha
Things you'll need
1. Picture box (To display the captcha image)
2. Two buttons (Refresh, and submitting answer)
3. Textbox (To type in the captcha answer)
To start out, right bellow Public Class Form1, put
Next we're going to make a function, which will create the captcha's question
Next we're going to create a private sub which will generate random lines around the captcha image
and finally we will create the function for generating the image itself.
Usage for this would be like this, The Submit button would have this code
The Refresh button would be this
and formload sub would contain this as well
Hope you enjoyed this tutorial!
Picture from Platinum:
![[Image: ivb3.png]](http://img842.imageshack.us/img842/4578/ivb3.png)
1. Picture box (To display the captcha image)
2. Two buttons (Refresh, and submitting answer)
3. Textbox (To type in the captcha answer)
To start out, right bellow Public Class Form1, put
Code:
Private GetAnswer As String = Nothing
Next we're going to make a function, which will create the captcha's question
Code:
Private Function QuestionMaker() As String
Dim OperatorsC As String() = {"+", "-"}
Start:
Dim Num1 As Integer = New Random().Next(1, 9)
Dim Num2 As Integer = New Random().Next(1, 9)
If Num1 = Num2 Then GoTo Start
Dim OperatorC2 As String = OperatorsC(New Random().Next(0, OperatorsC.Length))
Select Case OperatorC2
Case "+"
GetAnswer = Num1 + Num2
If GetAnswer <= 0 Then GoTo Start
Case "-"
GetAnswer = Num1 - Num2
If GetAnswer <= 0 Then GoTo Start
End Select
Return String.Format("{0}{1}{2}", Num1, OperatorC2, Num2)
End Function
Next we're going to create a private sub which will generate random lines around the captcha image
Code:
Private Sub CreateTheLines(ByVal G As Graphics)
If Not G Is Nothing Then
Dim R As New Random()
Dim lineBrush As New SolidBrush(Color.LightGray)
For i% = 0 To 9
G.DrawLines(New Pen(lineBrush, R.Next(1, 2)), New Point() {New Point(0, R.Next(0, 60)), New Point(200, R.Next(0, 60))})
Next
End If
End Sub
and finally we will create the function for generating the image itself.
Code:
Private Function ImageCreation() As Image
Dim B As New Bitmap(200, 60)
Using G As Graphics = Graphics.FromImage(B)
With G
.Clear(Color.White)
.DrawString(QuestionMaker(), New Font("Verdana", 20), Brushes.Black, New Rectangle(0, 0, 200, 60), New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
End With
CreateTheLines(G)
End Using
Return B
End Function
Usage for this would be like this, The Submit button would have this code
Code:
Select Case TextBox1.Text
Case Is = GetAnswer
MessageBox.Show("Correct!", "Correct!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
PictureBox1.Image = ImageCreation()
TextBox1.Clear()
Case Else
MessageBox.Show("Incorrect!", "Incorrect!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
PictureBox1.Image = ImageCreation()
TextBox1.Clear()
End Select
The Refresh button would be this
Code:
PictureBox1.Image = ImageCreation()
and formload sub would contain this as well
Code:
PictureBox1.Image = ImageCreation()
Hope you enjoyed this tutorial!
Picture from Platinum:
![[Image: ivb3.png]](http://img842.imageshack.us/img842/4578/ivb3.png)
![[Image: OkXCq.gif]](http://i.imgur.com/OkXCq.gif)