ALERT!
Click here to register with a few steps and explore all our cool stuff we have to offer!
Home
Upgrade
Credits
Help
Search
Awards
Achievements
 4345

[VB.NET]Creating a Basic Captcha String

by 1UP - 10-10-2016 - 11:24 PM
#1
This was something I pulled from a test program I was messing with. I was writing an application to send some information to a remote database and to prevent "flooding" I could write in a captcha. Granted I would never include this because of how simple it is, it was rather easy.

First off our function to create the random string of letters.
Code:
Private Function generateCaptchaString()
        Dim rand As New Random
        Dim currentLetter As String
        Dim captchaString As String = ""
        For i = 0 To 6
            currentLetter = ChrW(rand.Next(Asc("A"), Asc("Z") + 1))
            captchaString &= currentLetter
        Next

        Return captchaString
    End Function

Then the code for creating the image with the string as apart of it.
Code:
'Set captchaText to what is generated in the function generateRandomString
        Dim captchaText As String = generateCaptchaString()

        'Creating a bitmap in memory
        Dim captchaBitmap As New Bitmap(200, 40)

        'Creating a Graphic so we can manipulate how the image will look
        Dim captchaGraphic As Graphics = Graphics.FromImage(captchaBitmap)

        Dim captchaFont As New Font("Arial", 14)

        'Set the Text color
        Dim captchaForeColor As New SolidBrush(Color.Blue)

        'Set the background color
        Dim captchaBackgroundColor As New SolidBrush(Color.Yellow)

        captchaGraphic.FillRectangle(captchaBackgroundColor, 0, 0, Width, Height)
        captchaGraphic.DrawString(captchaText, captchaFont, captchaForeColor, 5.0F, 5.0F)

        pict_Captcha.Image = captchaBitmap
Reply
#2
(10-10-2016 - 11:24 PM)1UP Wrote: This was something I pulled from a test program I was messing with. I was writing an application to send some information to a remote database and to prevent "flooding" I could write in a captcha. Granted I would never include this because of how simple it is, it was rather easy.

First off our function to create the random string of letters.
Code:
   Private Function generateCaptchaString()
       Dim rand As New Random
       Dim currentLetter As String
       Dim captchaString As String = ""
       For i = 0 To 6
           currentLetter = ChrW(rand.Next(Asc("A"), Asc("Z") + 1))
           captchaString &= currentLetter
       Next

       Return captchaString
   End Function

Then the code for creating the image with the string as apart of it.
Code:
       'Set captchaText to what is generated in the function generateRandomString
       Dim captchaText As String = generateCaptchaString()

       'Creating a bitmap in memory
       Dim captchaBitmap As New Bitmap(200, 40)

       'Creating a Graphic so we can manipulate how the image will look
       Dim captchaGraphic As Graphics = Graphics.FromImage(captchaBitmap)

       Dim captchaFont As New Font("Arial", 14)

       'Set the Text color
       Dim captchaForeColor As New SolidBrush(Color.Blue)

       'Set the background color
       Dim captchaBackgroundColor As New SolidBrush(Color.Yellow)

       captchaGraphic.FillRectangle(captchaBackgroundColor, 0, 0, Width, Height)
       captchaGraphic.DrawString(captchaText, captchaFont, captchaForeColor, 5.0F, 5.0F)

       pict_Captcha.Image = captchaBitmap

This is a very nice tutorial man, very helpful. Thanks for sharing this method. Heart
Are you a guest to this site? Click the image below and sign up today!
[Image: FTYbRmR.gif]
Reply

Users browsing: 2 Guest(s)