![]() |
|
[Tutorial] Home made Captcha w/ user input - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Visual Basic & .NET Framework (https://sinister.li/Forum-Visual-Basic-NET-Framework) +--- Thread: [Tutorial] Home made Captcha w/ user input (/Thread-Tutorial-Home-made-Captcha-w-user-input) |
[Tutorial] Home made Captcha w/ user input - Hexology - 07-10-2013 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 Code: Private GetAnswer As String = NothingNext 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 FunctionNext 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 Suband 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 FunctionUsage 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 SelectThe 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: ![]() RE: [Tutorial] Home made Captcha w/ user input - ZherkTG - 07-11-2013 thanks for the tutorial i might try this soon RE: [Tutorial] Home made Captcha w/ user input - Hexology - 07-11-2013 (07-11-2013, 08:26 AM)ZherkTG Wrote: thanks for the tutorial i might try this soon Np, glad you liked it! RE: [Tutorial] Home made Captcha w/ user input - Platinum - 07-11-2013 Great tutorial man, works flawless! Here's the proof for the people that want to see it: http://prntscr.com/1erlg3 I have one tip for you guys: If you want to display the image correctly in pretty much any size then click on the litte arrow of the picturebox in the top right when you've selected the picture box. Change 'SizeMode' to 'Center Image'. RE: [Tutorial] Home made Captcha w/ user input - Hexology - 07-11-2013 (07-11-2013, 10:16 AM)Platinum Wrote: Great tutorial man, works flawless! Here's the proof for the people that want to see it: Ahh good I'll add the picture to the OP!
|