Simple Captcha Application
Posted: Sat Mar 30, 2013 2:54 am
This Simple Captcha Application doesn't need Internet to load it's Captcha but it generates a random image for you to answer.
Screeny: Okay, you need the following elements:
Button 2 will be the one refreshing a new generated image for you to answer.
TextBox you need this to answer lmao;
PictureBox you need this to show the image lmao;
Okay, let's start. Add this code below your Public Class.
This code will create the question and the lines so it's a bit harder to read which makes it a bit challenging.
Screeny: Okay, you need the following elements:
- 2 Button
1 PictureBox
1 TextBox
Button 2 will be the one refreshing a new generated image for you to answer.
TextBox you need this to answer lmao;
PictureBox you need this to show the image lmao;
Okay, let's start. Add this code below your Public Class.
This code will create the question and the lines so it's a bit harder to read which makes it a bit challenging.
Code: Select all
So next, we will be coding the submit button or Button 1, this will submit your answer. lmao;
Private cAnswer As String = Nothing
Private Function genQuestion() As String
Dim cOperators As String() = {"+", "-"}
Start:
Dim p1 As Integer = New Random().Next(1, 9)
Dim p2 As Integer = New Random().Next(1, 9)
If p1 = p2 Then GoTo Start
Dim cOperator As String = cOperators(New Random().Next(0, cOperators.Length))
Select Case cOperator
Case "+"
cAnswer = p1 + p2
If cAnswer <= 0 Then GoTo Start
Case "-"
cAnswer = p1 - p2
If cAnswer <= 0 Then GoTo Start
End Select
Return String.Format("{0}{1}{2}", p1, cOperator, p2)
End Function
Private Sub generateLines(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
Private Function generateImage() As Image
Dim B As New Bitmap(200, 60)
Using G As Graphics = Graphics.FromImage(B)
With G
.Clear(Color.White)
.DrawString(genQuestion(), New Font("Segoe UI", 20), Brushes.Black, New Rectangle(0, 0, 200, 60), New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
End With
generateLines(G)
End Using
Return B
End Function
Code: Select all
Button 2 and Form_Load will be the same code. Add this line of code to Button 2 and Form_Load.
Select Case TextBox1.Text
Case Is = cAnswer
MessageBox.Show("Correct!", "Correct!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
PictureBox1.Image = generateImage()
TextBox1.Clear()
Case Else
MessageBox.Show("Incorrect!", "Incorrect!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
PictureBox1.Image = generateImage()
TextBox1.Clear()
End Select
TextBox1.Clear()
Code: Select all
When you are done, debug it and test. Thanks for reading and testing this thread, I'm pretty sure you've learned something new.PictureBox1.Image = generateImage()