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 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.
After it draws the letters, the routine draws some random lines to clutter the image and erases some other lines lines to introduce breaks in the text.
|
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
Dim x1 As Single
Dim y1 As Single
Dim x2 As Single
Dim y2 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
' Mess things up a bit.
For i = 1 To 10
x1 = Rnd * wid
y1 = Rnd * hgt
x2 = Rnd * wid
y2 = Rnd * hgt
pic.Line (x1, y1)-(x2, y2)
Next i
For i = 1 To 10
x1 = Rnd * wid
y1 = Rnd * hgt
x2 = Rnd * wid
y2 = Rnd * hgt
pic.Line (x1, y1)-(x2, y2), vbWhite
Next i
End Sub
|