Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake CAPTCHA images (version 1) in Visual Basic 6
DescriptionThis example shows how to make CAPTCHA images (version 1) in Visual Basic 6.
KeywordsCAPTCHA, Turing test, image, image processing, distort image
CategoriesGraphics, Software Engineering
 

CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) images are those distorted pictures of words that some Web sites make you enter to prove you are a human and not an automated process. The idea is to distort the characters in the image so it would be hard for an optical character recognition (OCR) application to read them but so it would still be easy for a person to read them.

This example draws randomly sized, moved, and rotated letters. The letters are large so they overlap in ways that would be hard for OCR software to recognize.

Subroutine MakeCaptchaImage divides the image area into regions for each character in the message. Then for each character, it picks a random font size, rotation, and location. It then uses subroutine DrawCenteredRotatedText to draw the character rotated and centered at the position.

 
Private Sub MakeCaptchaImage(ByVal pic As PictureBox, ByVal _
    txt As String, ByVal min_size As Integer, ByVal _
    max_size As Integer)
Dim wid As Single
Dim hgt As Single
Dim ch_wid As Single
Dim i As Integer
Dim font_size As Single
Dim ch As String
Dim X As Single
Dim Y As Single
Dim prev_angle As Single
Dim angle As Single

    ' See how much room is available for each character.
    wid = pic.ScaleWidth
    hgt = pic.ScaleHeight
    ch_wid = wid / Len(txt)

    ' Draw each character.
    prev_angle = 0
    pic.Cls
    Randomize
    For i = 1 To Len(txt)
        ' Get the character and font size.
        ch = Mid$(txt, i, 1)
        font_size = min_size + rnd * (max_size - min_size)

        ' Get the position.
        X = (i - 0.75 + rnd * 0.5) * ch_wid
        Y = hgt / 2 + rnd * (hgt - pic.ScaleY(font_size, _
            vbPoints, vbTwips))

        ' Get the angle.
        angle = prev_angle
        Do While Abs(angle - prev_angle) < 10
            angle = -20 + rnd * (20 - -20)
        Loop
        prev_angle = angle

        ' Draw the next character.
        DrawCenteredRotatedText picCaptcha, ch, X, Y, _
            angle, font_size
    Next i
End Sub
 
Because Visual Basic 6 doesn't draw text with smoothing, the result is a bit jagged. See Resize an image with antialiasing using GetDIBits and SetDIBits for one method of smoothing the image.

See Draw rotated text centered at a point in Visual Basic 6 for a description of subroutine MakeCaptchaImage.

 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated