Classic ASP CAPTCHA (Image Verification) using ASPJpeg

Classic ASP CAPTCHA (Image Verification) using ASPJpeg

I wrote this script many years ago when I needed a simple CAPTCHA generator for use with the classic ASP ASPJpeg component.  At the time it received a lot of interest and while obviously almost obsolete these days, I have posted it for anyone looking for something like this.  If you’re thinking of making the jump to PHP – which I have – I thoroughly recommend this book: [easyazon_link asin=”B000VZW34A” locale=”US” new_window=”yes” nofollow=”default” tag=”bomc-21″ add_to_cart=”default” cloaking=”default” localization=”default” popups=”no”]Beginning PHP4 (Programmer to Programmer)[/easyazon_link] – clear and programming oriented (none of the waffle you see in other beginner books).

Note: If you’re working with ASP components, you might find my Classic ASP Component Detection Script useful.

The Classic ASP Code

Simply save this code in an ASP file using whatever file name you prefer – I generally save it in a file called imageVerification.asp – we will call this as an image using (see the form example below). When a page loads and calls this script, it generates an image on the fly and stores the code that is displayed in that image in a session variable called verificationCode – this can then be evaluated on whatever script/page your form submits too.

'Written by Bob McKay, please leave copyright intact.
'Copyright Bob McKay 2009.  All Rights Reserved.
'Visit for Updates.

'*************** CODE BOX OPTIONS ***************
codeBoxWidth = 120
codeBoxHeight = 40
numberOfLetters = 4 '					Allow Minimum 20 Pixels Per Letter - See codeBoxWidth Property Above)
wantBorderRoundCodeBox = TRUE '		Put a Border Round the code box(MUST be TRUE or FALSE)
borderRoundCodeBoxColor = "&HBED2E9"
backGroundColor = "&HF0F5FB"
codeFontColor = "&H666666"
numbersAllowedInCode = FALSE'	Set to FALSE for a 'Letters Only' Code
'The fonts the script is allowed to select from - these MUST be installed on the Host computer
hostFontList = Array("Arial", "Courier New", "Helvetica", "Times New Roman", "Georgia", "Comic Sans MS")
minimumFontSize = 24
maximumFontSize = 32
randomUnderLined = FALSE'			Randomly underline letters for extra distortion

'*************** DISTORTION OPTIONS ***************
codeImageJPGQuality = 15' 		Lower this to allow JPG distortion of the codeBox
multiColorLinesOnTop = TRUE'	If set to FALSE, ON TOP line color defaults to code font color
numberOfLongLinesOnTop = 0'		Number of end to end lines drawn ON TOP of the code
numberOfRandomLinesOnTop = 0'	Number small lines drawn ON TOP of the code
multiColorLinesUnderneath = TRUE'	If set to FALSE, UNDERNEATH line color defaults to code font color
numberOfRandomLinesUnderneath = 2'	Number small lines drawn UNDERNEATH of the code

'*************** FUNCTIONS & SUBROUTINES ***************
SUB drawBorderRoundBox
	aspJpegImageObject.Canvas.Brush.Solid = FALSE ' Do NOT fill box with color when drawing 
	aspJpegImageObject.Canvas.Pen.Color = borderRoundCodeBoxColor
	aspJpegImageObject.Canvas.DrawBar 0, 0, codeBoxWidth, codeBoxHeight

SUB drawLines(numberOflongLines, numberOfShortLines, randomLineColor)
	aspJpegImageObject.Canvas.Pen.Color = codeFontColor
	FOR drawLongLines = 1 TO numberOflongLines
		IF randomLineColor = TRUE THEN aspJpegImageObject.Canvas.Pen.Color = "&H" & HEX(INT(RND*255)) & HEX(INT(RND*255)) & HEX(INT(RND*255))
		aspJpegImageObject.Canvas.DrawLine 5, INT(RND * 35), (codeBoxWidth - 5), INT(RND * 35)
	FOR drawShortLines = 1 TO numberOfShortLines
		IF randomLineColor = TRUE THEN aspJpegImageObject.Canvas.Pen.Color = "&H" & HEX(INT(RND*255)) & HEX(INT(RND*255)) & HEX(INT(RND*255))
		aspJpegImageObject.Canvas.DrawLine INT(RND*codeBoxWidth)+1, INT(RND * 35), INT(RND*codeBoxWidth)+1, INT(RND * 35)

SUB writeCodeToImage(randomCode)
	aspJpegImageObject.Canvas.Font.Color = codeFontColor
	aspJpegImageObject.Canvas.Font.Quality = 4' Antialiased
	aspJpegImageObject.Canvas.Font.BkMode = "Opaque" ' Required for Antialiasing
	aspJpegImageObject.Canvas.Font.BkColor = backGroundColor
	FOR currentCodeLetter = 1 TO LEN(randomCode) ' Loop Through Letters in Code
		IF flipACoinFiftyFifty THEN	aspJpegImageObject.Canvas.Font.Italic = TRUE
		IF flipACoinFiftyFifty THEN	aspJpegImageObject.Canvas.Font.Bold = TRUE
		IF flipACoinFiftyFifty AND randomUnderLined THEN	aspJpegImageObject.Canvas.Font.Underlined  = TRUE
		aspJpegImageObject.Canvas.Font.Family = hostFontList(INT(RND * (UBOUND(hostFontList)+1))) 'Randomly Select Font from Host Font List
		aspJpegImageObject.Canvas.Font.Size = INT(RND * ((maximumFontSize - minimumFontSize)+1)) + minimumFontSize'INT(RND * 12) + 18 'Randomly Choose Font Size 14 to 26
		aspJpegImageObject.canvas.print charXposition, 5, MID(randomCode, currentCodeLetter, 1)
		charXposition = charXposition + spacePerLetter' Move Cursor Along for Next Letter

FUNCTION flipACoinFiftyFifty()
	flipACoinFiftyFifty = FALSE
	IF RND() > 0.49 THEN	flipACoinFiftyFifty = TRUE

FUNCTION generateRandomCode()
	FOR placebo = 1 TO numberOfLetters
		IF flipACoinFiftyFifty OR numbersAllowedInCode = FALSE THEN
	        generateRandomCode = generateRandomCode & CHR(INT((26 * RND()) + 65))
			generateRandomCode = generateRandomCode & CHR(INT((10 * RND()) + 48))
	SESSION("verificationCode") = generateRandomCode

'*************** BUILD THE CODE BOX ***************
spacePerLetter = INT(codeBoxWidth / numberOfLetters)'				Calculate How Much Space Each Character Has
charXposition = 5'													Start Cursor 5 Pixels from the Left
Set aspJpegImageObject = Server.CreateObject("Persits.jpeg")'		Initialise ASPJpeg Ovject
aspJpegImageObject.New codeBoxWidth, codeBoxHeight, backGroundColor'Create A New Blank JPG based on Options
IF wantBorderRoundCodeBox THEN drawBorderRoundBox '					If a border is wanted, draw one.
CALL drawLines(0, numberOfRandomLinesUnderneath, multiColorLinesUnderneath)' Draw Some Distorting Lines OVER Verification Code
CALL writeCodeToImage(generateRandomCode())'						Call Function to Write Code, passing it randomCode
CALL drawLines(numberOfLongLinesOnTop, numberOfRandomLinesOnTop, multiColorLinesOnTop)' 
aspJpegImageObject.Quality = codeImageJPGQuality'					Set JPG Image Quality
aspJpegImageObject.SendBinary' 										Send the Output of this Script as a JPEG
Set aspJpegImageObject = NOTHING'									Close ASPJpeg Object

Example Form

This is a simple login form to illustrate how this could be used.

Verification Code

Evaluate the CAPTCHA

The captcha is evaluated on the script or page the form submits to – the form above submits to processLogin.asp page which would contain the following:

IF REQUEST("verificationCode") <> SESSION("verificationCode") AND SESSION("verificationCode") <> "" THEN
	failureReason = "Incorrect verification code. Please enter the code displayed in to the 'Verify Code' box."
	Response.Redirect("login.asp?error=" & failureReason)
Bob McKay

About Bob McKay

Bob is a Founder of Seguro Ltd, a full time father and husband, part-time tinkerer-with-wires, coder, Muay Thai practitioner, builder and cook. Big fan of equality, tolerance and co-existence.

Disclosure Policy

9 comments on «Classic ASP CAPTCHA (Image Verification) using ASPJpeg»

  1. Agos says:

    Your example doesnt’ work

  2. Susan says:

    I am using the above code for Captcha but it seems sometimes the image for number or alphabet is not showing and iyt comes blank or just with one and when we try to submit it it wont submit since the code is not matching with what we submit can you help on this please

    1. Bob McKay says:

      Hi Susan,

      This is a *very* old piece of code so I’m not really that familiar with it anymore! Which font are you using and is ASPJpeg installed?


      1. Susan says:

        Hi Bob,
        Do you have newer version?


        1. Bob McKay says:

          I’m afraid not as Classic ASP is really a legacy language, this article was written almost ten years ago (it pains me to say that because it was my first love as far as server side languages go!).

      2. Susan says:

        Hi Bob,

        Its Canvas


        1. Bob McKay says:

          Hi Susan,

          Sorry I’m not understanding – what about Canvas?

        2. Niente says:

          Susan you can try this one, it needs no external components:

          1. Michal says:

            Dear Niente,
            i cant download it know, project web seems be dead. Dont You have the source code? Can You send it to me?

            Have a nice day

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.