What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake CAPTCHA images (version 5) in Visual Basic .NET
DescriptionThis example shows how to make CAPTCHA images (version 5) in Visual Basic .NET.
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.

Subroutine DrawCaptcha is quick and simple. It merely draws two strings on top of each other. Usually a human can still read them.

' Draw the words overlaid on each other.
Private Function MakeCaptchaImage(ByVal wid As Integer, _
    ByVal hgt As Integer, ByVal the_font As Font, ByVal _
    word1 As String, ByVal word2 As String) As Image
    Dim bm As New Bitmap(wid, hgt)
    Dim gr As Graphics = Graphics.FromImage(bm)

    ' Draw the first word, offset horizontally by a random
    ' amount.
    Dim size1 As SizeF = gr.MeasureString(word1, the_font)
    Dim rnd As New Random
    Dim x As Single = rnd.Next(0, CInt(wid - size1.Width))
    gr.DrawString(word1, the_font, Brushes.Blue, x, 0)

    ' Draw the second word over the first,
    ' offset vertically a bit and horizontally
    ' by a random amount.
    Dim size2 As SizeF = gr.MeasureString(word2, the_font)
    x = rnd.Next(0, CInt(wid - size2.Width))
    Dim y As Integer = CInt(size2.Height / 3)
    If y > CInt(hgt - size2.Height) Then _
        y = CInt(hgt - size2.Height)
    gr.DrawString(word2, the_font, Brushes.Blue, x, y)

    Return bm
End Function
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.