Imports System.Drawing.Drawing2D Partial Class Captcha Inherits System.Web.UI.Page Private Const CAPTCHA_WIDTH As Single = 180 Private Const CAPTCHA_HEIGHT As Single = 80 Private Const CAPTCHA_MSGLENGTH As Integer = 6 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim imgwidth As Integer = CType(CAPTCHA_WIDTH, Integer) Dim imgheight As Integer = CType(CAPTCHA_HEIGHT, Integer) Dim bmp As Bitmap bmp = New Bitmap(imgwidth, imgheight) Dim gfx As Graphics gfx = Graphics.FromImage(bmp) ' Gradient will clear picture! ' gfx.Clear(Color.Blue) Dim background As LinearGradientBrush background = New LinearGradientBrush(New Point(0, 0), New Point(imgwidth, 0), Color.Blue, Color.Black) background.WrapMode = WrapMode.TileFlipX Dim linecolour As Pen ' define colours Dim bgblend As New ColorBlend Dim bgsrccolours() As Color = {Color.Black, Color.Blue, Color.DarkGreen, Color.DarkMagenta, Color.DarkOrchid} Dim bgsrclinecolours() As Color = {Color.Green, Color.Blue, Color.Brown, Color.DarkMagenta} bgblend.Colors = New Color() {bgsrccolours(Int(Rnd() * (UBound(bgsrclinecolours) + 1))), bgsrccolours(Int(Rnd() * (UBound(bgsrclinecolours) + 1))), bgsrccolours(Int(Rnd() * (UBound(bgsrclinecolours) + 1)))} linecolour = New Pen(bgsrclinecolours(Int(Rnd() * (UBound(bgsrclinecolours) + 1)))) bgblend.Positions = New Single() {0.0, 0.5, 1.0} background.InterpolationColors = bgblend ' draw the background gfx.FillRectangle(background, 0, 0, imgwidth, imgheight) ' draw some gfx to mess up the display ' objGraphics.DrawLine(new Pen(Color.Red), 0, 0, 200, 200) Dim loopi As Integer For loopi = 0 To 12 gfx.DrawLine(linecolour, 0, CAPTCHA_HEIGHT * Rnd(), CAPTCHA_WIDTH, CAPTCHA_HEIGHT * Rnd()) gfx.DrawLine(linecolour, CAPTCHA_WIDTH * Rnd(), 0, CAPTCHA_WIDTH * Rnd(), CAPTCHA_HEIGHT) Next linecolour.Color = Color.White For loopi = 0 To 4 gfx.DrawLine(linecolour, 0, CAPTCHA_HEIGHT * Rnd(), CAPTCHA_WIDTH, CAPTCHA_HEIGHT * Rnd()) gfx.DrawLine(linecolour, CAPTCHA_WIDTH * Rnd(), 0, CAPTCHA_WIDTH * Rnd(), CAPTCHA_HEIGHT) Next gfx.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias ' create our message Dim msgpt1 As String Dim msgpt2 As String msgpt1 = "" msgpt2 = "" For loopi = 1 To CAPTCHA_MSGLENGTH / 2 msgpt1 = msgpt1 & Int(Rnd() * 10).ToString msgpt2 = msgpt2 & Int(Rnd() * 10).ToString Next ' font for our text Dim fontname1 As String Dim fontname2 As String Dim fontnames() As String = {"Arial", "Courier", "Times new Roman", "Georgia", "Trebuchet MS", "Impact", "MS Sans Serif", "Roman", "Comic Sans MS"} fontname1 = fontnames(Int(Rnd() * (UBound(fontnames) + 1))) fontname2 = fontnames(Int(Rnd() * (UBound(fontnames) + 1))) Dim style1 As FontStyle Dim style2 As FontStyle Dim styles() As FontStyle = {FontStyle.Bold, FontStyle.Italic, FontStyle.Regular, FontStyle.Underline} style1 = styles(Int(Rnd() * (UBound(styles) + 1))) style2 = styles(Int(Rnd() * (UBound(styles) + 1))) Dim fntpt1 As Font fntpt1 = New Font(fontname1, 32, style1) Dim fntpt2 As Font fntpt2 = New Font(fontname2, 32, style2) ' part 1 ' find a position for it Dim fntsize As SizeF fntsize = gfx.MeasureString(msgpt1, fntpt1) Dim fntwidth As Single = fntsize.Width Dim fntheight As Single = fntsize.Height fntsize = gfx.MeasureString(msgpt2, fntpt2) fntwidth += fntsize.Width fntheight += fntsize.Height Dim leftoffset As Single Dim topoffset As Single leftoffset = (CAPTCHA_WIDTH - fntwidth) / 2.0 topoffset = ((CAPTCHA_HEIGHT - fntheight) / 2.0) + (Rnd() * 20) + 12 ' + 12 stops negative slopes going off the top of the screen ' Bend it a bit! Dim rotation As Single rotation = ((Rnd() * 16) - 4) gfx.RotateTransform(rotation) ' draw our message gfx.DrawString(msgpt1, fntpt1, Brushes.White, leftoffset, topoffset) ' part 2 leftoffset = ((CAPTCHA_WIDTH - fntwidth) / 2.0) + (CAPTCHA_WIDTH / 2 + (Rnd() * 20)) topoffset = ((CAPTCHA_HEIGHT - fntheight) / 2.0) + (Rnd() * 20) + 12 ' transform it a bit! gfx.ResetTransform() ' inverse the old rotation gfx.RotateTransform(12 - rotation) gfx.ScaleTransform((Rnd() * 0.15) + 0.75, (Rnd() * 0.15) + 0.75) ' draw our message gfx.DrawString(msgpt2, fntpt2, Brushes.White, leftoffset, topoffset) ' save our msg Session.Add("CAPTCHA_TEXT", msgpt1 & msgpt2) ' set the content type Response.ContentType = "image/GIF" ' output our image bmp.Save(Response.OutputStream, ImageFormat.Gif) ' clear up fntpt1.Dispose() fntpt2.Dispose() gfx.Dispose() bmp.Dispose() End Sub End Class