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

Create Captcha with Refresh Button in ASP.NET

Article:
Viewed:  2525 
Posted On:  21/09/2014 08:38:59 
How we create a captcha image in asp.net c# ? How we can referesh a captcha image in asp.net 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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();
        }
    </script>
 
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="10" cellspacing="10" width="50%" style="border: solid 10px Green;
            background-color: skyblue;" align="center">
            <tr>
                <td align="right">
                    Captcha Image #
                </td>
                <td align="left" valign="middle">
                    <img src="CaptchaHandler.ashx" id="imgCaptchaShow" /><br />
                    <br />
                    <a href="#" onclick="javascript:RefreshCaptcha();">Refresh</a>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Here I am using an ImageHandler.ashx

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

using
System;
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";
                context.Response.End();
            }
        }
    }
 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    } 
}

Now Run Application :

1.png

Image 1.

2.png

Image 2.

  Comment:
 
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