How To Make A Captcha Generator In VB.NET

We all know what is captcha if you don't know what is, captcha is a word test or more than one word test that is mixed and cannot identify easily that is used to verify human. Okay now we are going to make it first you need to add a class so go ahead and add a class and add the codes below to the class.





Imports System.Text
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D

Class Captcha

    Inherits PictureBox
    Public randomstr As String
#Region "Properties"
    Private _textcolor As Color = Color.Black
    Public Property TextColor() As Color
        Get
            Return _textcolor
        End Get
        Set(ByVal value As Color)
            _textcolor = value
            Invalidate()
        End Set
    End Property

    Private _font As New Font("Segoe UI", 20)
    Public Overrides Property Font() As Font
        Get
            Return _font
        End Get
        Set(ByVal value As Font)
            _font = value
            Invalidate()
        End Set
    End Property

    Private _texttouse As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz1234567890"
    Public Property RandomCharacters() As String
        Get
            Return _texttouse
        End Get
        Set(ByVal value As String)
            _texttouse = value
            Invalidate()
        End Set
    End Property

    Private _captchalength As Integer = 8
    Public Property CaptchaTextLength() As Integer
        Get
            Return _captchalength
        End Get
        Set(ByVal value As Integer)
            _captchalength = value
            Invalidate()
        End Set
    End Property

    Private _numberoflines As Integer = 50
    Public Property NumberOfLines() As Integer
        Get
            Return _numberoflines
        End Get
        Set(ByVal value As Integer)
            _numberoflines = value
            Invalidate()
        End Set
    End Property
#End Region

#Region "Events"
    Public Sub New()
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.ResizeRedraw, True)
        SetStyle(ControlStyles.UserPaint, True)
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        SetStyle(ControlStyles.SupportsTransparentBackColor, True)
    End Sub

    Protected Overrides Sub CreateHandle()
        MyBase.CreateHandle()
        Me.Size = New Size(241, 69)
        Me.BorderStyle = BorderStyle.FixedSingle
    End Sub

    Public CaptchaText As String = String.Empty
    Private rnd As New Random()
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Dim B As New Bitmap(Width, Height)
        Dim G As Graphics = Graphics.FromImage(B)

        Dim BoxSize As New Rectangle(0, 0, Me.Width, Me.Height)
        Dim txtcolor As Brush = New SolidBrush(Color.FromArgb(rnd.[Next](160, 255), _textcolor))
        G.SmoothingMode = SmoothingMode.HighQuality

        G.Clear(BackColor)

        For I As Integer = 0 To _numberoflines - 1
            G.DrawLine(New Pen(Color.FromArgb(rnd.[Next](128, 255), CreateRandomColor())), rnd.[Next](0, BoxSize.Width), rnd.[Next](0, BoxSize.Height), rnd.[Next](BoxSize.Width), rnd.[Next](BoxSize.Height))
        Next

        Dim SFormat As New StringFormat()
        SFormat.Alignment = StringAlignment.Center
        SFormat.LineAlignment = StringAlignment.Center

        G.RotateTransform(rnd.[Next](-7, 7), MatrixOrder.Append)

        randomstr = CreateRandomString()
        G.DrawString(randomstr, Font, txtcolor, BoxSize, SFormat)
        CaptchaText = randomstr

        e.Graphics.DrawImage(B, 0, 0)
        G.Dispose()
        B.Dispose()
    End Sub
#End Region

#Region "Create Randoms"
    Private Function CreateRandomColor() As Color
        Return Color.FromArgb(100, rnd.[Next](255), rnd.[Next](255), rnd.[Next](255))
    End Function

    Private Function CreateRandomString() As String
        Dim buffer As Char() = New Char(_captchalength - 1) {}
        For i As Integer = 0 To _captchalength - 1
            If _texttouse = String.Empty Then
                Dim newtext As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz1234567890"
                buffer(i) = newtext(rnd.[Next](newtext.Length))
            Else
                buffer(i) = _texttouse(rnd.[Next](_texttouse.Length))
            End If
        Next
        Return New String(buffer)
    End Function
#End Region
End Class
After that debug your project and now there will be a new tool in your toolbox called "Captcha" now you need two buttons and a text box so drag and drop them into the form and now add the Captcha tool inside the form .

Change the text property of button 1 to Submit and change the text property of button 2 to Reset and now make your form look like this

To check the captcha, let's code double click Submit button and add this code
  If TextBox1.Text = Captcha1.CaptchaText Then
            MsgBox("Correct Input", MsgBoxStyle.Information)
        Else
            MsgBox("Wrong Input", MsgBoxStyle.Critical)
        End If
Double click Reset button and add this code
        Captcha1.Refresh()
Finally debug and have fun

13 comments

Sorry if this is a bad question, but how do you take this very nice captcha that you built and make it available as a website? Popup? Any direction on how to do that would help.

Reply

This can only be used in VB.NET and maybe in ASP.NET, i am not sure about it.

Reply

Thanks . ;)

Reply

I've used in vb.net desktop application and works!!! not fine but works!!!!!

Reply

Hi, Nice captcha you have there, I have few questions and need your help, can you help with this ? I'm facing problem with the class code. For this line of code, error is detected' and is expecting '>' as well as for this code <.span id="IL_AD1" class="IL_AD">Public randomstr As String. For this code <.span id="IL_AD8" class="IL_AD">Bitmap(Width, Height), Type is expected. Span is not allowed here, so i purposely put a dot beside the span. Thank You.

Reply

It's some HTML tags get into the codes section that's the problem

Reply

I see, thank you :)

Reply

i cant get a solution for the captcha1.CaptchaText in the (button1_click)it shows error wherever the captcha1 is present please reply me asap

Reply

thanks a lot it works a great

Reply

It is a great think of the VB.net using captcha text generator with good work for the secure with the page are sites. It do for pleasant work in captcha coding. I like do these programming.

Dotnet Training in Chennai

Reply

my last comment was bad. this is a good works!!!!

Reply

I have read your blog its very attractive and impressive. I like it your blog.

Dot Net Training in Chennai Dot Net Training in Chennai .Net Online Training .Net Online Training Dot Net Training in Chennai

Dot Net Online Training .Net Online Training ASP.NET Online Training ASP.NET Online Training

Reply

You.
Thinking there might be someone who is reading this and who knows that I am right this very moment as I write this loving you so much, just unconditionally and wholeheartedly. Yes, I thought. I am lonely. Yes, I sometimes wish life were more straightforward and easy, but you are helping me by existing. You are giving me such a gift letting me love you right now.
(And always, getting appropriate medication and counseling is top of a to-do list about suicidal feelings or depression. I’m a huge fan of the Caron Institute for recommendations on good counselors in your area and as a wonderful support network in general. And please don't rule out medication like I did for most of my adult years. I find my life so much better on Zoloft, and it took me more than 30 years to consider the route. There are also so many awesome support groups that are life changing. If one sucks, it's OK. Go to the next one! That sucks, too? Cool. Maybe try another one. Maybe don't. Action steps are great, though. So give yourself that credit for even thinking about taking them. You're not alone, friend. I swear to God you are not.)

ADVERTISING

inRead invented by Teads
And while you’re waiting to get your first therapy appointment, maybe, just maybe, something from this list will remind you of stuff you like out there in this world.

Reply

Post a Comment

Note: Only a member of this blog may post a comment.