Click here to Skip to main content
13,044,467 members (116,568 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 11 Oct 2013

Image Recaptcha using Generic Handler

, 18 Oct 2013
Rate this:
Please Sign up or sign in to vote.

Editorial Note

This articles was originally at but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Generic handler for captcha image generation:
<%@ WebHandler Language="C#" Class="CaptchaHandler" %> 
    using System; 
    using System.Web; 
    using System.Drawing; 
    using System.Drawing.Text; 
    using System.Drawing.Imaging; 
    using System.Drawing.Drawing2D; 
    using System.IO; 
    using System.Web.SessionState;

public class CaptchaHandler : IHttpHandler, IRequiresSessionState
        public void ProcessRequest(HttpContext context)
            using (Bitmap b = new Bitmap(250, 50))
                Font f = new Font("Arial Black", 20F);
                Graphics g = Graphics.FromImage(b);
                SolidBrush whiteBrush = new SolidBrush(Color.Black);
                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
                return false;

        private string GetRandomString()
            string[] arrStr = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,A,R,S,T,U,V,W,X,Y,Z,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;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

ASP.NET Community
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
Group type: Collaborative Group

765 members

You may also be interested in...


Comments and Discussions Pin
Nimishkumar14-Oct-13 10:36
professionalNimishkumar14-Oct-13 10:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 18 Oct 2013
Article Copyright 2013 by ASP.NET Community
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid