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.
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.
Description:
In previous posts I explained group columns in asp.net gridview header row, jQuery change tooltip style in asp.net, C# add rows from one datatable to another datatable, asp.net get all files from folder and display it in gridview and many articles relating to Asp.net, Gridview, c#, vb.net. Now I will explain how to create captcha in asp.net using c#, vb.net.
In previous posts I explained group columns in asp.net gridview header row, jQuery change tooltip style in asp.net, C# add rows from one datatable to another datatable, asp.net get all files from folder and display it in gridview and many articles relating to Asp.net, Gridview, c#, vb.net. Now I will explain how to create captcha 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. |
|||
|
|||
18 comments :
great
superb, your posts are very useful, please keep posting.
-Abdul Samathu K
Dear admin can u help me?
Do you have sample project to consume this?
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???
Can you show how to integrate this by setting the text from the page and having the page matching the code entered?
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
Gand tuji
hi i want to check the captcha string in textbox when inserting values how to do ?help me.......
How to validate the input string with captcha string?
Sir, i have implemented above example . is doing well. but how to verify input captcha on button click?
Superb..
How can i check captcha image and text endered in textbox ?
how i can validate capcha hero????????????????????????????????????????
Great post how we can apply validation on Captcha image ??
Reply if ASAP if possible
Sir ,
This is very good but how to get value of this captcha in textbox
please send at sonupandey991@gmail.com
thanks
How can i check captcha image and text ?
How can i check captcha image and text check in c# form
retrive hanler
Note: Only a member of this blog may post a comment.