Code:
Function fncDrawCaptcha(ByVal path As String) As String
'*** Editable Values
Dim BackgroundColor As [String]() = New [String]() {255, 255, 255} ' The 3 numbers represent in order RED, GREEN, BLUE for the captcha's background color
Dim RandomBackgroundNoiseColor As Boolean = True ' True / False. If you choose True, BackgroundNoiseColor will not apply
Dim RandomTextColor As Boolean = True ' True / False. If you choose True, TextColor will not apply
Dim BackgroundNoiseColor As [String]() = New [String]() {150, 150, 150} ' The 3 numbers represent in order RED, GREEN, BLUE
Dim TextColor As [String]() = New [String]() {200, 200, 200} ' The 3 numbers represent in order RED, GREEN, BLUE
Dim BackgroundNoiseTexture As HatchStyle = HatchStyle.Min ' replace ".Min" with any of the following: Horizontal, Vertical, ForwardDiagonal, BackwardDiagonal, Cross, DiagonalCross, Percent05, Percent10, Percent20, Percent25, Percent30, Percent40, Percent50, Percent60, Percent70, Percent75, Percent80, Percent90, LightDownwardDiagonal, LightUpwardDiagonal, DarkDownwardDiagonal, DarkUpwardDiagonal, WideDownwardDiagonal, WideUpwardDiagonal, LightVertical, LightHorizontal, NarrowVertical, NarrowHorizontal, DarkVertical, DarkHorizontal, DashedDownwardDiagonal, DashedUpwardDiagonal, DashedHorizontal, DashedVertical, SmallConfetti, LargeConfetti, ZigZag, Wave, DiagonalBrick, HorizontalBrick, Weave, Plaid, Divot, DottedGrid, DottedDiamond, Shingle, Trellis, Sphere, SmallGrid, SmallCheckerBoard, LargeCheckerBoard, OutlinedDiamond, SolidDiamond, LargeGrid, Min, Max
Dim length as integer = 6 ' Number of characters to generate
'*** END Editable Values
Dim height as integer = 100
Dim width as integer = 200
width = width + ((length - 6) * 30)
Dim ranRotate As New Random
Dim strText As String = left(replace(System.Guid.NewGuid().ToString(), "-", ""), length)
Dim bmpCanvas As New Bitmap(width, height, PixelFormat.Format24bppRgb)
Dim graCanvas As Graphics = Graphics.FromImage(bmpCanvas)
Dim recF As New RectangleF(0, 0, width, height)
Dim bruBackground As Brush
Dim letterBrush as SolidBrush
graCanvas.TextRenderingHint = TextRenderingHint.AntiAlias
If RandomBackgroundNoiseColor = true Then
bruBackground = New HatchBrush(BackgroundNoiseTexture, Color.FromArgb((ranRotate.Next(0,255)), (ranRotate.Next(0,255)), (ranRotate.Next(0,255)) ), Color.FromArgb(BackgroundColor(0),BackgroundColor(1),BackgroundColor(2)))
Else
bruBackground = New HatchBrush(BackgroundNoiseTexture, Color.FromArgb(BackgroundNoiseColor(0), BackgroundNoiseColor(1), BackgroundNoiseColor(2) ), Color.FromArgb(BackgroundColor(0),BackgroundColor(1),BackgroundColor(2)))
End If
graCanvas.FillRectangle(bruBackground, recF)
If RandomTextColor = true Then
letterBrush = new SolidBrush(Color.FromArgb((ranRotate.Next(0,255)), (ranRotate.Next(0,255)), (ranRotate.Next(0,255)) ))
Else
letterBrush = new SolidBrush(Color.FromArgb(TextColor(0), TextColor(1), TextColor(2)))
End If
Dim matRotate As New System.Drawing.Drawing2D.Matrix
Dim i As Integer
For i = 0 To Len(strText) - 1
matRotate.Reset()
matRotate.RotateAt(ranRotate.Next(-30, 30), New PointF(width/(Len(strText) + 1) * i, height * 0.5))
graCanvas.Transform = matRotate
If i = 0 Then
graCanvas.DrawString(strText.Chars(i), New Font("Comic Sans MS", 25, FontStyle.Italic), letterBrush, width/(Len(strText) + 1) * i, height * 0.4) 'draw ‘the text on our image
ElseIf i = 1 Then
graCanvas.DrawString(strText.Chars(i), New Font("Arial", 30, FontStyle.Bold), letterBrush, width/(Len(strText) + 1) * i, height * 0.1) 'draw ‘the text on our image
ElseIf i = 2 Then
graCanvas.DrawString(strText.Chars(i), New Font("Times New Roman", 25, FontStyle.Italic), letterBrush, width/(Len(strText) + 1) * i, height * 0.5) 'draw ‘the text on our image
ElseIf i = 3 Then
graCanvas.DrawString(strText.Chars(i), New Font("Georgia", 35, FontStyle.Bold), letterBrush, width/(Len(strText) + 1) * i, height * 0.1) 'draw ‘the text on our image
ElseIf i = 4 Then
graCanvas.DrawString(strText.Chars(i), New Font("Verdana", 25, FontStyle.Italic), letterBrush, width/(Len(strText) + 1) * i, height * 0.5) 'draw ‘the text on our image
ElseIf i = 5 Then
graCanvas.DrawString(strText.Chars(i), New Font("Geneva", 30, FontStyle.Bold), letterBrush, width/(Len(strText) + 1) * i, height * 0.1) 'draw ‘the text on our image
Else
graCanvas.DrawString(strText.Chars(i), New Font("Arial", 30, FontStyle.Italic), letterBrush, width/Len(strText) * i, height * 0.5) 'draw ‘the text on our image
End If
graCanvas.ResetTransform()
Next
bmpCanvas.Save(path, ImageFormat.Gif)
graCanvas.Dispose()
bmpCanvas.Dispose()
Return strText
End Function
Sub subCheckCaptcha(sender As Object, e As System.EventArgs)
If Not txtCaptcha.Text = Session.Item("strText") Then
lblMessage.text = "The characters does not match"
Else
lblMessage.text = ""
subSubmitForm()
End If
End Sub
Sub subSubmitForm()
'Your code in here
response.write("Captcha text entered successfully")
End Sub
Use the following controls in your form section to display the Captcha to your website users