Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


[Tutorial] Home made Captcha w/ user input filter_list
Author
Message
[Tutorial] Home made Captcha w/ user input #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
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]
[Image: OkXCq.gif]

[+] 1 user Likes Hexology's post
Reply

RE: [Tutorial] Home made Captcha w/ user input #2
thanks for the tutorial i might try this soon

Reply

RE: [Tutorial] Home made Captcha w/ user input #3
(07-11-2013, 08:26 AM)ZherkTG Wrote: thanks for the tutorial i might try this soon

Np, glad you liked it!
[Image: OkXCq.gif]

Reply

RE: [Tutorial] Home made Captcha w/ user input #4
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'.

Reply

RE: [Tutorial] Home made Captcha w/ user input #5
(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:
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'.

Ahh good Smile I'll add the picture to the OP!
[Image: OkXCq.gif]

Reply







Users browsing this thread: 1 Guest(s)