Creating simple Captcha Image using Bitmap class and Graphics Class. RRS feed

  • Question

  • User2118976055 posted

    Hi All,

    The below code helped me to create the simple captcha image dynamically.

    If anyone want to know how to create and use captch image for their web application Please see the below code and it will give

    some basic Idea about that.




    objGraphics.TextRenderingHint = System.Drawing.Text.Btmap objBMP = new Bitmap(100, 40);Graphics objGraphics = Graphics.FromImage(objBMP);Color.Aqua);TextRenderingHint.ClearTypeGridFit;//' Configure font to use for text 

    Font objFont = new Font("verdana", 15, FontStyle.Regular);string randomStr = "";char[] myArray = new char[5];int x;//That is to create the random # and add it to our string 


    myArray[x] = System.

    randomStr += (myArray[x].ToString());

    }Random autoRand = new Random();for (x = 0; x < 5; x++)Convert.ToChar(autoRand.Next(65, 90));//This is to add the string to session, to be compared later

    Session.Add("RandomStr", randomStr);//' Write out the text

    objGraphics.DrawString(randomStr, objFont,Brushes.Red, 10, 8);//' Set the content type and return the image

    Response.ContentType = "image/GIF";

    objBMP.Save(Response.OutputStream, System.Drawing.Imaging."image/GIF";ImageFormat.Gif);




    Hope This will help some one to get some Idea about Captcha.


    Tuesday, April 27, 2010 6:50 AM

All replies

  • User-446933086 posted


    This really niece idea but one thing i want to ask. will we have to save this image on web server or this image will be directly send to client side ?

    please elaborate and one thing more please tell what will be its effect of this when we are using ajax, will this automatically updated.

    Friday, April 30, 2010 1:36 AM
  • User2118976055 posted

    Hi Idressbari,

    We will have to save this image on web server,after that we can use where ever we need.

    and i dint have any idea about using Ajax.

    Friday, April 30, 2010 2:20 AM
  • User2118976055 posted


    I used in my website by using Iframe concept.

    we need to save on the web server so that only we can use it where ever we need.

    I am not having any idea what you are mentioning about AJAX.

    Friday, April 30, 2010 2:24 AM
  • User350563001 posted

    you can use some captcha image ajax control in you asp.net application and it also automatically update its content when the page is refreshed or reload 

    Thursday, May 20, 2010 11:29 PM