Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

Create Captcha with Refresh Button in ASP.NET

Viewed:  5070 
Posted On:  21/09/2014 08:38:59 
How we create a captcha image in c# ? How we can referesh a captcha image in c# ? 

In this article I am going to show how we can create captcha image and how we can refresh on button click.

Below is my aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>Captcha in ASP.NET C#</title>

    <script type="text/javascript">
        function RefreshCaptcha() {
            var img = document.getElementById("imgCaptchaShow");
            img.src = "CaptchaHandler.ashx?query=" + Math.random();
    <form id="form1" runat="server">
        <table cellpadding="10" cellspacing="10" width="50%" style="border: solid 10px Green;
            background-color: skyblue;" align="center">
                <td align="right">
                    Captcha Image #
                <td align="left" valign="middle">
                    <img src="CaptchaHandler.ashx" id="imgCaptchaShow" /><br />
                    <br />
                    <a href="#" onclick="javascript:RefreshCaptcha();">Refresh</a>

Here I am using an ImageHandler.ashx

@ WebHandler Language="C#" Class="CaptchaHandler" %>

using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
public class CaptchaHandler : IHttpHandler
    public void ProcessRequest(HttpContext context)
        using (Bitmap b = new Bitmap(150, 40, PixelFormat.Format32bppArgb))
            using (Graphics g = Graphics.FromImage(b))
                Rectangle rect = new Rectangle(0, 0, 149, 39);
                g.FillRectangle(Brushes.White, rect);
                // Create string to draw.
                Random r = new Random();
                int startIndex = r.Next(1, 5);
                int length = r.Next(5, 10);
                String drawString = Guid.NewGuid().ToString().Replace("-", "0").Substring(startIndex, length);
                // Create font and brush.
                Font drawFont = new Font("Arial", 16, FontStyle.Italic | FontStyle.Strikeout);
                using (SolidBrush drawBrush = new SolidBrush(Color.Black))
                    // Create point for upper-left corner of drawing.
                    PointF drawPoint = new PointF(15, 10);
                    // Draw string to screen.
                    g.DrawRectangle(new Pen(Color.Red, 0), rect);
                    g.DrawString(drawString, drawFont, drawBrush, drawPoint);
                b.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                context.Response.ContentType = "image/jpeg";
    public bool IsReusable
            return false;

Now Run Application :


Image 1.


Image 2.

By engmg  On  28/10/2016 08:28:57
How should the CAPTCHA code is entered correctly?
By engmg  On  28/10/2016 08:24:43
How should the CAPTCHA code is entered correctly?
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us