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

Sep 26, 2013

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

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

<html xmlns="">
<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();
<form id="form1" runat="server">
<img src="Handler.ashx" id="imgCaptcha" />
<a href="#" onclick="javascript:RefreshCaptcha();">Refresh</a>
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";
public bool IsReusable {
get {
return false;

<%@ 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"
End Using
End Using
End Sub

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

End Class

Download Sample Code Attached

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

Get the latest,, 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


Anonymous said...


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?

Lakhpat Singh said...

Dear Sir,
Your all post are very superb and useful, I thanks for all.
Please send me an email to for: How to Verify the Captcha code in 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...


RAHUL303 said...

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

pin 5 said...

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

Anonymous said...

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

Welcome To EvenementDesigners | Events said...

Sir ,
This is very good but how to get value of this captcha in textbox
please send at

Give your Valuable Comments

Other Related Posts

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