Post: [VB.NET]Creating a Basic Captcha String
07-25-2012, 05:51 AM #1
Default Avatar
Oneup
Guest
(adsbygoogle = window.adsbygoogle || []).push({}); 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.
        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.
            '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

The following 3 users say thank you to Oneup for this useful post:

One, Toke, UnofficialMWS
12-21-2014, 03:04 AM #2
Not Rage
Can’t trickshot me!
I know how to add this in c# already but how will I code it to confirm the sites captcha ?
12-21-2014, 09:50 AM #3
Default Avatar
Oneup
Guest
Originally posted by RAGEMODDZz View Post
I know how to add this in c# already but how will I code it to confirm the sites captcha ?


This doesn't have anything to do with a web site unless you are doing a website in ASP.NET and if that is the case I wouldn't use this since there are better options.

As for using this in a regular windows form you compare what you have to the string that was generated.

    
Dim captchaText As String 'I made this a global variable in case I wanted to use the captcha string elsewhere

    
captchaText = generalFunctions.generateCaptchaString()
If captchaText <> txt_Captcha.Text Then
'Insert error message here
End If

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo