ASp.Net day

Micro blog



About Satalaj

www.satalaj.com

The best inline translator

Live lookup to see what asp.net developers are searching




C# Simple Captcha

kick it on DotNetKicks.com
aspnet captcha

  In this post I will create simple CAPTCHA image using ASP.Net with C#.net and VB.net.
CAPTCHA is used to identify end user as a human. History can be refereed here http://en.wikipedia.org/wiki/CAPTCHA

This article will help you to convert String to Image or Convert Text to image

Idea is system will store randomly generated text into session. later it would be rendered as a image (text to image)

The working copy can be downloaded from here  captcha.rar (2.44 kb) 






Our aspx page would look like this

 


<
form id="form1" runat="server">

<div>

<img src="Handler.ashx" />

<br />

Please type above text here. To identify your self as a human

<br />

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />

<br />

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>

</form>


In above code   this image tag  <img src="Handler.ashx" />    will request  Handler

Lets add Generic handler to your new asp.net project. IE or FF end user browser will request this handler code.
 

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;

public class Handler : IHttpHandler,IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        using (Bitmap b = new Bitmap(250, 50))
        {
            Font f = new Font("Arial", 10F);
            Graphics g = Graphics.FromImage(b);
            SolidBrush whiteBrush = new SolidBrush(Color.Blue);
            SolidBrush blackBrush = new SolidBrush(Color.White);
            RectangleF canvas = new RectangleF(0, 0, 250, 50);
            g.FillRectangle(whiteBrush, canvas);           
            context.Session["Captcha"] = GetRandomString();
            g.DrawString(context.Session["Captcha"].ToString(), f, blackBrush, canvas);
            context.Response.ContentType = "image/gif";
            b.Save(context.Response.OutputStream, ImageFormat.Gif);
        }
    }
   
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
   
    private string GetRandomString()
    {
        string []arrStr = "A,B,C,D,1,2,3,4,5,6,7,8,9,0".Split(",".ToCharArray());
        string strDraw = string.Empty;
        Random r = new Random();        
         for(int i = 0; i < 5 ; i++)
         {
              strDraw += arrStr[r.Next(0,arrStr.Length-1)];
         }       
        return strDraw;
    }
}
 




Now open your default.aspx page code behind where we have added Captcha image tag

on button click validate whether user has entered correct text that is rendered by system or not 

 

protected void Button1_Click(object sender, EventArgs e)
    {
        if(Session["Captcha"].ToString() == TextBox1.Text)
        {
            Label1.Text = " System identified you as a human";
        }
        else
        {
            Label1.Text = "Please try again";
            TextBox1.Text="";
        }
    }