Archive

Archive for August, 2008

Captcha Using Asp.Net

August 25th, 2008

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

[Post to Twitter]   [Post to Plurk]   [Post to Digg]   [Post to ping.fm]

Related posts

aby ASP.net

.NET Framework 3.5 Common Namespaces and Types Poster

August 11th, 2008

The .NET Framework 3.5 Common Namespaces and Types Poster is downloadable as XPS or PDF format. Print one and hang it on your wall :-) Download it here

[Post to Twitter]   [Post to Plurk]   [Post to Digg]   [Post to ping.fm]

Related posts

Shoban links, vb.net

Ajax using post

August 2nd, 2008

Here is a sample code which uses POST method in XMLHTTP request.

var url="ajaxpage.php";
var parms="name=bobby"; //Parameters
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", parms.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = function() {
             //On ready state execute the below code
	     if(http.readyState == 4 || http.status == 200) {
		alert(xmlHttp.responseText);
	}
}
xmlHttp.send(parms);

[Post to Twitter]   [Post to Plurk]   [Post to Digg]   [Post to ping.fm]

Related posts

Shoban Uncategorized

5 Ajax calendars you shouldn’t miss

August 1st, 2008
  1. Vista Like Ajax calendar : The Vista-like Ajax Calendar (vlaCalendar) version 2 is a unobtrusive  web version of the slick and profound Windows Vista taskbar calendar, by using the mootools javascript framework, AJAX, XHTML, CSS and PHP. Read more here
  2. Aeron Calendar : Calendar is a Javascript class that adds accessible and unobtrusive date-pickers to your form elements. This class is a compilation of many date-pickers I have implemented over the years and has been completely re-written for Mootools. I have tried to include all the features that have been most useful while streamlining the class itself to keep it as small as possible. Use the links below to see what features are available in Calendar and how it might enhance the accessibility, usability and validation of form elements on your website. Read more
  3. Ajax calendar for ASP.net
  4. Super Ajax calendar : The super version includes the posting and viewing of linked events, and it comes with an admin tool to easily add and modify events to the calendar. Also added is a “jump to” box (the down arrow) so you can jump to a month / year instead of having to navigate using the previous / next functions. Read more and download here
  5. Simple Ajax calendar : Get it here

[Post to Twitter]   [Post to Plurk]   [Post to Digg]   [Post to ping.fm]

Related posts

Shoban links, php