Thursday, September 25, 2008

How to Make a Captcha Image in C# .NET



What Captcha stand for?
Completely Automated Public Turing test to tell Computers and Humans Apart.
The Captcha technology help you to make sure your site is reasonably secure against automated attacks.

Write the following code in a class named Captcha :
public class Captcha
{
    //make the captcha image for text
    public Bitmap MakeCaptchaImage(string txt, int width, int hight, string fontFamilyName)
    {
        //make the bitmap and the associated Graphics object
        Bitmap bm = new Bitmap(width, hight);
        Graphics gr = Graphics.FromImage(bm);
        gr.SmoothingMode = SmoothingMode.HighQuality;
        RectangleF recF = new RectangleF(0, 0, width, hight);
        Brush br;
        br = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White);
        gr.FillRectangle(br, recF);
        SizeF text_size;
        Font the_font;
        float font_size = hight + 1;
        do
        {
            font_size -= 1;
            the_font = new Font(fontFamilyName, font_size, FontStyle.Bold, GraphicsUnit.Pixel);
            text_size = gr.MeasureString(txt, the_font);
        }
        while ((text_size.Width > width) || (text_size.Height > hight));
        // Center the text.
        StringFormat string_format = new StringFormat();
        string_format.Alignment = StringAlignment.Center;
        string_format.LineAlignment = StringAlignment.Center;

        // Convert the text into a path.
        GraphicsPath graphics_path = new GraphicsPath();
        graphics_path.AddString(txt, the_font.FontFamily, 1, the_font.Size, recF, string_format);

        //Make random warping parameters.
        Random rnd = new Random();
        PointF[] pts = { new PointF((float)rnd.Next(width) / 4, (float)rnd.Next(hight) / 4), new PointF(width - (float)rnd.Next(width) / 4, (float)rnd.Next(hight) / 4), new PointF((float)rnd.Next(width) / 4, hight - (float)rnd.Next(hight) / 4), new PointF(width - (float)rnd.Next(width) / 4, hight - (float)rnd.Next(hight) / 4) };
        Matrix mat = new Matrix();
        graphics_path.Warp(pts, recF, mat, WarpMode.Perspective, 0);

        // Draw the text.
        br = new HatchBrush(HatchStyle.LargeConfetti, Color.LightGray, Color.DarkGray);
        gr.FillPath(br, graphics_path);

        // Mess things up a bit.
        int max_dimension = System.Math.Max(width, hight);
        for (int i = 0; i <= (int)width * hight / 30; i++)
        {
            int X = rnd.Next(width);
            int Y = rnd.Next(hight);
            int W = (int)rnd.Next(max_dimension) / 50;
            int H = (int)rnd.Next(max_dimension) / 50;
            gr.FillEllipse(br, X, Y, W, H);
        }
        for (int i = 1; i <= 5; i++)
        {
            int x1 = rnd.Next(width);
            int y1 = rnd.Next(hight);
            int x2 = rnd.Next(width);
            int y2 = rnd.Next(hight);
            gr.DrawLine(Pens.DarkGray, x1, y1, x2, y2);
        }
        for (int i = 1; i <= 5; i++)
        {
            int x1 = rnd.Next(width);
            int y1 = rnd.Next(hight);
            int x2 = rnd.Next(width);
            int y2 = rnd.Next(hight);
            gr.DrawLine(Pens.LightGray, x1, y1, x2, y2);
        }
        graphics_path.Dispose();
        br.Dispose();
        the_font.Dispose();
        gr.Dispose();
        return bm;
    }
}
Write the following code in the page load event:
Captcha oCaptcha = new Captcha();
Random rnd = new Random();
string[] s = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
int i;
StringBuilder sb = new StringBuilder(4);
for (i = 0; i <= 4; i++)
{
    sb.Append(s[rnd.Next(1, s.Length)]);
}
Bitmap bm = oCaptcha.MakeCaptchaImage(sb.ToString(), 200, 100, "Arial");
string url = Server.MapPath("~/Images/Captcha.bmp");
bm.Save(url);
Image1.ImageUrl = "~/Images/Captcha.bmp";

6 comments:

  1. tslm dma3'k bgd
    ana kont m7tag a3rf el fekra bta3ha mn zmaaan
    wz amr saafan mosh hat2dr t3'md 3neek

    ReplyDelete
  2. Nice article! However, the codes are not properly placed. It is a good idea to put it into Code Snippet.

    You can download Microsoft Live Writer to help you with it. Im using it for my blog too. Nice tool.

    Regards,
    Chris
    http://chrispar.blogspot.com

    ReplyDelete
  3. Thank you Chris Par for your good advice

    ReplyDelete
  4. ((text_size.Width > width)(text_size.Height > height))

    is missing the ||(Or operator) in the C# sample. Probably an encoding anomaly.

    ReplyDelete
  5. Yes, true the ||(Or operator) was missing and I updated the article now.
    Thank you.

    ReplyDelete
  6. It was and with me. Let's discuss this question. Here or in PM.

    ReplyDelete