Categories
Asp, Asp.net

Captcha Example In Asp.Net C#

Captcha Example In Asp.Net C#

In this tutorial i will explain about Captcha,and how to use Captcha Example In Asp.Net C#.We will use Generic Handler to achieve this.

Design:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Captcha Example In Asp.Net C#</title>
<style type="text/css">
.bdy
{
margin: 0px auto;
width:900px;
font-family:Calibri;
font-size:14px;
padding-top:100px;
}
</style>
<script type="text/javascript">
function Captcha() {
var img = document.getElementById("img");
img.src = "Captcha.ashx?query=" + Math.random();
}
</script>
</head>
<body class="bdy">
<form id="form1" runat="server">

<div style="text-align:center">
<h1>Captcha Example In Asp.Net C#</h1>
<img src="Captcha.ashx" id="img" style="padding-top:40px" />
<a href="#" onclick="javascript:Captcha();"><img src="refresh.png" style="padding-top:-10px" /></a>
</div>
</form>
</body>
</html>

Handler Code:

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

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;

public class Captcha : IHttpHandler {
    
public void ProcessRequest (HttpContext context) {
   using (Bitmap b = new Bitmap(70, 25, PixelFormat.Format32bppArgb))
   {
    using (Graphics g = Graphics.FromImage(b))
    {
     Rectangle rect = new Rectangle(0, 0, 70, 25);
     g.FillRectangle(Brushes.White, rect);
     Random r = new Random();
     int startIndex = r.Next(1, 5);
     int length = r.Next(4, 7);
     String drawString = Guid.NewGuid().ToString().Replace("-", "0").Substring(startIndex, length);
     Font drawFont = new Font("Calibri", 14);
     using (SolidBrush drawBrush = new SolidBrush(Color.Black))
     {
         PointF drawPoint = new PointF(0, 0);
         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;
    }
    }
}

Captcha Example In Asp.Net C#    Captcha Example In Asp.Net C#

6 replies on “Captcha Example In Asp.Net C#”

That is really attention-grabbing, You’re a very professional blogger.
I have joined your rss feed and sit up for searching for extra
of your excellent post. Additionally, I’ve shared your web site in my social networks

Thanks in support of sharing such a nce thinking, piece of writing is nice, thats why i hve read iit completely

Hi, I ԁο thinκ thіs is
an excellеnt wеb sіte. I stumbledupon it ;
) I mау reѵisit yyet аgain ѕince I saѵeԁ as a faνorite it.

Comments are closed.