Aug 25 2008

Captcha Using Asp.Net

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Here is a simple code to create a captcha image. This ASP.net code creates a bitmap image of a randomly selected string. I think this code is self explanatory. If you have any queries please post it as a comment I will reply.Below are some of the images generated by this code

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
 
Imports System.Drawing.Text
Imports System.Data.OleDb
 
Partial Class _Default
    Inherits System.Web.UI.Page
 
    ''' <summary>
    ''' This function will create a bitmap from the given string and implement random changes on that bitmap to make it a capcha.
    ''' </summary>
    ''' <param name="sImageText"></param>
    ''' <returns>bitmap image</returns>
    ''' <remarks></remarks>
    Private Function CreateImage(ByVal sImageText As String)
 
        Dim bmpImage As New Bitmap(1, 1)
        Dim iWidth As Integer = 0
        Dim iHeight As Integer = 0
        Dim RandomInst As New Random
        Dim iMinNoice As Integer = RandomInst.Next(6, 9)
        Dim iMaxNoice As Integer = RandomInst.Next(9, 11)
        Dim iScrachCode As Integer = RandomInst.Next(3, 6)
        Dim iVerHorRand As Integer = RandomInst.Next(1, 4)
        Dim iColor As System.Drawing.Color = GetColorFromID(RandomInst.Next(1, 7))
        Dim i_backColor As Color = Nothing
        'Create a Font object
 
        Dim MyFont As New Font(GetRandomFontName, 30, System.Drawing.FontStyle.Bold + _
                        System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point)
 
        Dim MyGraphics As Graphics
        MyGraphics = Graphics.FromImage(bmpImage)
 
        'Resize the bitmap, this is where the bitmap is sized.
        iWidth = CInt(MyGraphics.MeasureString(sImageText, MyFont).Width)
        iHeight = CInt(MyGraphics.MeasureString(sImageText, MyFont).Height)
 
        bmpImage = New Bitmap(bmpImage, New Size(iWidth, iHeight - 1))
 
 
 
        MyGraphics = Graphics.FromImage(bmpImage)
 
        MyGraphics.Clear(Color.White)
        i_backColor = bmpImage.GetPixel(0, 0)
 
 
        MyGraphics.TextRenderingHint = TextRenderingHint.AntiAlias
        MyGraphics.DrawString(sImageText, MyFont, New SolidBrush(iColor), 0, 0)
 
        'Put random pixels on the image
        For m As Integer = 1 To iHeight - 3
            For n As Integer = 1 To iWidth - 3
                If m Mod RandomInst.Next(iMinNoice, iMaxNoice) = 0 Then
                    bmpImage.SetPixel(n, m, iColor)
                ElseIf n Mod RandomInst.Next(iMinNoice, iMaxNoice) = 0 Then
                    bmpImage.SetPixel(n, m, iColor)
                End If
            Next
        Next
 
        'draw random bars on the image
        For m As Integer = 1 To iHeight - 3
            For n As Integer = 1 To iWidth - 3
                If m Mod 10 = 0 Then
                    bmpImage.SetPixel(n, m, iColor)
                ElseIf n Mod 10 = 0 Then
                    bmpImage.SetPixel(n, m, iColor)
                End If
            Next
        Next
        'draw random graphics on the bitmap
        Select Case iVerHorRand
            Case 1
                For m As Integer = 1 To iHeight - 3
                    For n As Integer = 1 To iWidth - 3
                        If bmpImage.GetPixel(n, m) <> i_backColor AndAlso _
                            m Mod (iScrachCode * iScrachCode) = 0 Then
                            bmpImage.SetPixel(n, m, i_backColor)
                        End If
                    Next
                Next
            Case 2
                For m As Integer = 1 To iHeight - 3
                    For n As Integer = 1 To iWidth - 3
                        If bmpImage.GetPixel(n, m) <> i_backColor AndAlso _
                            n Mod (iScrachCode * iScrachCode) = 0 Then
                            bmpImage.SetPixel(n, m, i_backColor)
                        End If
                    Next
                Next
            Case 3
                iScrachCode = RandomInst.Next(4, 7)
                For m As Integer = 1 To iHeight - 3
                    For n As Integer = 1 To iWidth - 3
                        If bmpImage.GetPixel(n, m) <> i_backColor AndAlso _
                            m Mod iScrachCode = 0 AndAlso n Mod iScrachCode = 0 Then
                            bmpImage.SetPixel(n, m, i_backColor)
                        End If
                    Next
                Next
        End Select
 
        MyGraphics.Flush()
        Return bmpImage
 
    End Function
    ''' <summary>
    ''' This function will return the name of the font as per the given ID
    ''' </summary>
    ''' <param name="a_FontID"></param>
    ''' <returns>Name of the font as per the given ID</returns>
    ''' <remarks></remarks>
    Private Function GetFontNameFromID(ByVal a_FontID As Integer) As String
        GetFontNameFromID = Nothing
 
        Select Case a_FontID
            Case 1 : GetFontNameFromID = "Goudy Stout"
            Case 2 : GetFontNameFromID = "Copperplate Gothic Bold"
            Case 3 : GetFontNameFromID = "Eras Bold ITC"
            Case 3 : GetFontNameFromID = "Blue Highway Linocut"
            Case 4 : GetFontNameFromID = "Rockwell Extra Bold"
            Case 4 : GetFontNameFromID = "Eras Bold ITC"
            Case 5 : GetFontNameFromID = "Verdana"
            Case 6 : GetFontNameFromID = "Algerian"
            Case 7 : GetFontNameFromID = "Bodoni MT Black"
            Case 8 : GetFontNameFromID = "Cooper Black"
            Case 10 : GetFontNameFromID = "Gill Sans Ultra Bold"
            Case 11 : GetFontNameFromID = "Rockwell Extra Bold"
            Case 12 : GetFontNameFromID = "Showcard Gothic"
            Case 13 : GetFontNameFromID = "Berlin Sans FB Demi"
 
        End Select
 
    End Function
 
    Private Function GetCharFromID(ByVal a_CharID As Integer) As String
        GetCharFromID = " "
        'This function return a charecter according to the value of the variable "a_CharID"
        'Here some confucing letters like I,l and 1 are avoided
        Select Case a_CharID
            Case 1 To 2 : GetCharFromID = Chr(76)
 
            Case 3 To 10 : GetCharFromID = Chr(a_CharID + 47)
 
            Case 11 To 18 : GetCharFromID = Chr(a_CharID + 54)
 
            Case 19 : GetCharFromID = Chr(a_CharID + 53)
 
            Case 20 To 36 : GetCharFromID = Chr(a_CharID + 54)
 
            Case 37 To 47 : GetCharFromID = Chr(a_CharID + 60)
 
            Case 48 : GetCharFromID = Chr(a_CharID + 59)
 
            Case 49 To 63 : GetCharFromID = Chr(a_CharID + 60)
 
        End Select
    End Function
 
    ''' <summary>
    ''' Return a random color as per the color ID
    ''' </summary>
    ''' <param name="a_ColorId"></param>
    Private Function GetColorFromID(ByVal a_ColorId As Integer) As Color
        GetColorFromID = Color.Green
 
        Select Case a_ColorId
            Case 1 : GetColorFromID = Color.Green
            Case 2 : GetColorFromID = Color.Blue
            Case 3 : GetColorFromID = Color.Red
            Case 4 : GetColorFromID = Color.Violet
            Case 5 : GetColorFromID = Color.Black
            Case 6 : GetColorFromID = Color.RosyBrown
            Case 7 : GetColorFromID = Color.Brown
            Case 8 : GetColorFromID = Color.Navy
            Case 8 : GetColorFromID = Color.Plum
        End Select
 
    End Function
    ''' <summary>
    ''' This function will generate a random string with length of a_Length
    ''' </summary>
    ''' <param name="a_Length"></param>
    Private Function GetRandomString(ByVal a_Length As String) As String
        GetRandomString = ""
        Dim RandomInst As New Random
        'GetCharFromID() function will returns 63 different charecters according 
        'to the ID passing to the function. Here we are calling the function 5 times 
        'with a random number as ID and thus creating a charecter string with 5 
        'random charecters.
 
        For i As Integer = 1 To a_Length
            GetRandomString = GetRandomString + GetCharFromID(RandomInst.Next(1, 63)) '+ " "
        Next
    End Function
 
    Private Function GetRandomFontName() As String
        GetRandomFontName = ""
        Dim RandomInst As New Random
        GetRandomFontName = GetFontNameFromID(RandomInst.Next(1, 13))
 
    End Function
 
 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.ContentType = "image/gif"
        Dim bmpImage As New Bitmap(1, 1)
        Dim strImage As String = GetRandomString(5) 'Create a random string with the length of 5 charecter
 
        'Call the CreateImage() function to save the random string to a stream which will be
        'drawn in the CreateImage() function.
        bmpImage = CreateImage(strImage)
        Dim a As String = ""
        bmpImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif)
        Response.End()
    End Sub
End Class

Add to Del.cio.us RSS Feed Add to Technorati Favorites Stumble It! Digg It!
    www.sajithmr.com

1 Comments on this post

Trackbacks

  1. Twitter / shoban: Fiddling with my blog post:... wrote:
    [...] with my blog post: Captcha Using Asp.Net ( http://www.codegeeks.net/?p=92 [...]
    August 25th, 2008 at 11:01 pm

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog