Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Asp.net Create Captcha with Refresh Button in C#, VB.NET

Sep 26, 2013
Introduction:

Here I will explain how to create captcha in asp.net using c#, vb.net or create captcha with refresh button in asp.net using c#, vb.net.

To implement this first we need to new website and design your aspx page like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Captcha Image with Refresh Button</title>
<script type="text/javascript">
function RefreshCaptcha() {
var img = document.getElementById("imgCaptcha");
img.src = "Handler.ashx?query=" + Math.random();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="Handler.ashx" id="imgCaptcha" />
<a href="#" onclick="javascript:RefreshCaptcha();">Refresh</a>
</div>
</form>
</body>
</html>
After completion of aspx page design need to add one handler file in your website for that Right click on website -à Select Add New Item -à Select Generic Handler file and give name as Handler.ashx and click ok

Now open Handler.ashx file and write the following code

C# Code


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

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

public class Handler : 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;
}
}
}
VB.NET Code


<%@ WebHandler Language="VB" Class="Handler2" %>
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Web

Public Class Handler2 : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Using b As New Bitmap(150, 40, PixelFormat.Format32bppArgb)
Using g As Graphics = Graphics.FromImage(b)
Dim rect As New Rectangle(0, 0, 149, 39)
g.FillRectangle(Brushes.White, rect)

' Create string to draw.
Dim r As New Random()
Dim startIndex As Integer = r.[Next](1, 5)
Dim length As Integer = r.[Next](5, 10)
Dim drawString As [String] = Guid.NewGuid().ToString().Replace("-", "0").Substring(startIndex, length)

' Create font and brush.
Dim drawFont As New Font("Arial", 16, FontStyle.Italic Or FontStyle.Strikeout)
Using drawBrush As New SolidBrush(Color.Black)
' Create point for upper-left corner of drawing.
Dim drawPoint As New PointF(15, 10)

' Draw string to screen.
g.DrawRectangle(New Pen(Color.Red, 0), rect)
g.DrawString(drawString, drawFont, drawBrush, drawPoint)
End Using
b.Save(context.Response.OutputStream, ImageFormat.Jpeg)
context.Response.ContentType = "image/jpeg"
context.Response.[End]()
End Using
End Using
End Sub

Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property

End Class
Demo


Download Sample Code Attached


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

18 comments :

Anonymous said...

great

Anonymous said...

superb, your posts are very useful, please keep posting.

-Abdul Samathu K

Sandeep Soni said...

Dear admin can u help me?

Anonymous said...

Do you have sample project to consume this?

Anonymous said...

hey suresh am new to http handlers how do i return the draw string with the image so i can match users input with the captcha???

Anonymous said...

Can you show how to integrate this by setting the text from the page and having the page matching the code entered?

Unknown said...

Dear Sir,
Your all post are very superb and useful, I thanks for all.
Please send me an email to maclibrary40@gmail.com for: How to Verify the Captcha code in asp.net C#
Thanks in advance,
From: Lakhpat Singh

Anonymous said...

Gand tuji

Anonymous said...

hi i want to check the captcha string in textbox when inserting values how to do ?help me.......

Anonymous said...

How to validate the input string with captcha string?

Sumeet said...

Sir, i have implemented above example . is doing well. but how to verify input captcha on button click?

Anonymous said...

Superb..

RAHUL303 said...

How can i check captcha image and text endered in textbox ?

Unknown said...

how i can validate capcha hero????????????????????????????????????????

Anonymous said...

Great post how we can apply validation on Captcha image ??
Reply if ASAP if possible

Business Central | Navision said...

Sir ,
This is very good but how to get value of this captcha in textbox
please send at sonupandey991@gmail.com
thanks

Anonymous said...

How can i check captcha image and text ?

Anonymous said...

How can i check captcha image and text check in c# form
retrive hanler

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.