Arithmetic Captcha

Posted by Venkat | Labels: , ,

In this post i am going to show how to work with Arithmetic CAPTCHA. Generally we used , Alpha or alphaNumeric Captcha on most of the site but some site like  ASP.SNIPPETS it shows ARITHMETIC CAPTCHA like 18 + 12 = ? so user have to give correct value then only it proceeds.

Here I am getting help from this site

http://www.knowlegezone.com/documents/80/Simple-ASPNET-CAPTCHA-Tutorial/

Which was in VB.NET so I would like to Written in C#, here I have been posted , On the Above link they used normal ASPX image to Generate the Image ie: 18 + 12 = .

So i written the code on Generic Handler File to improve the site performance.
By default Handler file does not Read or write the value to Session so we have to use like this

using System.Web.SessionState;

public class Captcha : IHttpHandler, IRequiresSessionState


suppose if we are going to read the Session value on HTTP Handler file

using System.Web.SessionState;
public class Captcha : IHttpHandler, IReadOnlySessionState

This is the full code for Captcha.ashx file

<%@ WebHandler Language="C#" Class="Captcha" %>
using System;
using System.Web;
using System.Drawing;
using System.Web.SessionState;
public class Captcha : IHttpHandler, IRequiresSessionState
{
   
    public void ProcessRequest (HttpContext context) {
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        Random num1 = new Random();
        Random num2 = new Random();
        int numQ1 = 0;
        int numQ2 = 0;
        string QString = null;
        numQ1 = num1.Next(10, 15);
        numQ2 = num1.Next(17, 31);
        QString = numQ1.ToString() + " + " + numQ2.ToString() + " = ";
        int tAnswer = numQ1 + numQ2;
        context.Session["answer"] = tAnswer .ToString ();
        Bitmap bitmap = new Bitmap(85, 25);
        Graphics Grfx = Graphics.FromImage(bitmap);
        Font font = new Font("Arial", 18, FontStyle.Bold, GraphicsUnit.Pixel);
        Rectangle Rect = new Rectangle(0, 0, 100, 25);
        Grfx.FillRectangle(Brushes.Snow, Rect);
        Grfx.DrawRectangle(Pens.White, Rect);
        // Border
        Grfx.DrawString(QString, font, Brushes.Black, 0, 0);
        context.Response.ContentType = "Image/jpeg";
        bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        bitmap.Dispose();
        Grfx.Dispose();
    }
    public bool IsReusable {
        get {
            return false;
        }
    }
}

Here is the code if you are going to write it on Captcha.aspx page

Explanation was given on Comment itself. Write the below code on Page_Load Event

// Getting Random number
        Random num1 = new Random();
        Random num2 = new Random();

        int numQ1 = 0;
        int numQ2 = 0;
        string QString = null;

        // here we get the random number for the first and second numer - ie: shows the number in between the range.
        numQ1 = num1.Next(10, 15);
        numQ2 = num1.Next(17, 31);

        //Total answer has been  stored it on String and assign to the Session["number"]
        QString = numQ1.ToString() + " + " + numQ2.ToString() + " = ";
        Session["answer"] = numQ1 + numQ2;

        // Here we create  Bitmap width - 85 and height - 25
        Bitmap bitmap = new Bitmap(85, 25);
        Graphics Grfx = Graphics.FromImage(bitmap);

        // Setting the font name, size etc for the text that we have to write it on the image
        Font font = new Font("Arial", 18, FontStyle.Bold, GraphicsUnit.Pixel);

        // Here we specify  a Rectangle object of x , y co-ordinate , with width and height 
        Rectangle Rect = new Rectangle(0, 0, 100, 25);

        //Fill the color to the Rectangle
        Grfx.FillRectangle(Brushes.Snow  ,Rect);

        // Now Using Pen Object Drawing a rectangle
        Grfx.DrawRectangle(Pens.White, Rect);

        // Border - here drawing the string of the num1 and num 2, font size , type etc, font color , and x and y co-ordinate
        Grfx.DrawString(QString, font, Brushes.Black   , 0, 0);
        // Specify the Content type of the image - here i am using JPEG
        Response.ContentType = "Image/jpeg";

        // Save the image and show it on page using response object
        bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

        //Dispose the object and release the resources
        bitmap.Dispose();
        Grfx.Dispose();

PayOffers.in