Al Pascual



Geo RSS
Geo Twitter Timeline

Blogs I read

<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Code Snip Collection "Creating CAPTCHA Like Words "

From

Mohammad Azam

http://forums.asp.net/1482564/ShowThread.aspx 

CaptchaControl.cs:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;

public partial class CaptchaControl : System.Web.UI.Page
{
    private Random rand = new Random();
  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {        
           CreateImage();          
        }
    }

    private void CreateImage()
    {
        string code = GetRandomText();
       
        Bitmap bitmap = new Bitmap(200,150,System.Drawing.Imaging.PixelFormat.Format32bppArgb);       

        Graphics g = Graphics.FromImage(bitmap);
        Pen pen = new Pen(Color.Yellow);
        Rectangle rect = new Rectangle(0,0,200,150);

        SolidBrush b = new SolidBrush(Color.DarkKhaki);
        SolidBrush blue = new SolidBrush(Color.Blue);
      
        int counter = 0;
      
        g.DrawRectangle(pen, rect);
        g.FillRectangle(b, rect);

        for (int i = 0; i < code.Length; i++)
        {
            g.DrawString(code[i].ToString(), new Font("Verdena", 10 + rand.Next(14, 18)), blue, new PointF(10 + counter, 10));
            counter += 20;
        }

        DrawRandomLines(g);

        bitmap.Save(Response.OutputStream,ImageFormat.Gif);

        g.Dispose();
        bitmap.Dispose();
       
    }

    private void DrawRandomLines(Graphics g)
    {
        SolidBrush green = new SolidBrush(Color.Green);
        for (int i = 0; i < 20; i++)
        {
            g.DrawLines(new Pen(green, 2), GetRandomPoints());
        }

    }

    private Point[] GetRandomPoints()
    {
        Point[] points = { new Point(rand.Next(10, 150), rand.Next(10, 150)), new Point(rand.Next(10, 100), rand.Next(10, 100)) };
        return points;
    }

    private string GetRandomText()
    {
        StringBuilder randomText = new StringBuilder();
       
        if (Session["Code"] == null)
        {
            string alphabets = "abcdefghijklmnopqrstuvwxyz";
           
            Random r = new Random();
            for (int j = 0; j <= 5; j++)
            {

                randomText.Append(alphabets[r.Next(alphabets.Length)]);
            }

            Session["Code"] = randomText.ToString();
        }

        return Session["Code"] as String;
    }

}

 

Default.aspx.cs(This pages uses the image and validates it):

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
           
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        // validate the code

        string userEnteredCode = TextBox1.Text;

        ValidateUserCode(userEnteredCode);
    }

    private void ValidateUserCode(string userEnteredCode)
    {
        if (Session["Code"].ToString().Equals(userEnteredCode))
        {
            Response.Write("Nice to know that you are not a bot.");
        }
        else
        {
            // clear the session and generate a new code
            Session["Code"] = null;
        }
    }
}

Comments

vikram said:

Good example
# December 2, 2006 1:25 AM

Azam said:

First of all I would like to thank Albert for posting the code in his blog. I would also like to invite everyone to read my latest article http://gridviewguy.com/ArticleDetails.aspx?articleID=223 which, explains the concept in more details. Enjoy!
# December 3, 2006 1:50 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# December 4, 2006 8:58 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# December 20, 2006 1:37 PM

oyun said:

Thanks very comment

# December 30, 2007 6:42 AM