Few days back, I got some question related to Captcha (security mechanism, which helps web masters to avoid spam) in a Forum. So I thought of implementing one. I got few nice scripts in Code Project, its a simple implementation, no too much logic and not too complex to understand. Also I am using an HTTP Handler instead of ASPX Page, for the implementation.
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();
}
}
I wrote the code in the Process Request event in HTTPHandler.
And to use this in your pages you can create an IMG tag with src attribute pointing to this.
<img src="myhandler.ashx" />
Note: Some time ASP.Net caches the image, so you may need to pass some GUID as querystring in the Handler.
Update: Sometime we may need to refresh the Captcha image, without post back, here is a simple javascript which will refresh the captcha image without post back.
<script type="text/javascript">
function RefreshCaptcha() {
var img = document.getElementById("imgCaptcha");
img.src = "Handler.ashx?query=" + Math.random();
}
</script>
<div>
<img src="Handler.ashx" id="imgCaptcha" />
<a href="#" onclick="javascript:RefreshCaptcha();">Refresh</a>
</div>
Here is the screenshot of web page using Captcha.

Jinesh B
Let me know how do we consume the value in this image with our asp.net page that is for checking the with captcha values and textbox values that the entered.
Anuraj P
We are using Session to manage it, before creating the image, we are setting the value to the session, and based on the session we are rendering image.
Radhika
Hello,
how can integrate this captcha code in aspx page. can you please tell me .
thank you,
Anuraj P
Hi Radhika
I am not clear with your requirement. You want how to implement the code? or You want to use ASPX instead of ASHX First option is simple and clearly mentioned in the post, like Create ASHX Page, copy / paste the code provided. Add an Image control in the ASPX Page, and set the Image source as ASHX that’s it. It will display the Image and you can check the value against the session variable. Yes you can use ASPX instead of ASHX, simply Copy / Paste the code to Page Load event of the Page, replace context with HttpContext.Current class. Rest is same as ASHX.
venkat
Hi Wish you a very happy new year.
Could you pls explain this part..
// 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);
I debugged this code but i didnt get it, i debugged and note downs the guid passed one and use substring to get specific character but it show different ..
Pls explain this…
Anuraj P
@venkat Its nothing; seems pretty self explanatory, I am randomly deciding the start index and length of the word to take it from GUID. If it contains a “-”, I am replacing it with 0. Hope you understood.