Captcha Image in ASP.Net

Nearly every website has the same style. When the user fills in an online form for an online registration for many things, such as an examination, booking a ticket online or any kind of money transaction, an image is shown in the registration form. When you have successfully filled in the specified words (provided in the image) in the TextBox, your form is submitted.

Captcha image in Asp.Net

These kinds of images are called CAPTCHA images. In this article, I will show you how to implement a CAPTCHA feature in your ASP.Net web form application.

For using this feature, we have the "mscaptcha.dll" file. You can download this file from this article.

Question: What is the "MSCaptcha.dll" file?

Answer: The "MSCaptcha.dll" file provides a user control for randomly generating CAPTCHA images.

So let's start implementation of CAPTCHAs in our website.

  1. Open Visual Studio
  2. "File" -> "New" -> "Project..." then seelct "ASP.Net Webform Application"
  3. Add a new web form
  4. Download "Mscaptcha.dll"
  5. Now right-click on the project and select "Add Reference" then select "mscaptcha.dll" from where you stored it then click on "Ok".

    Captcha image1

    You can see in the following image that "MScaptcha.dll" is loaded into your project.

    Captcha image2
  6. If you are developing your application in Visual Studio 2012 or later then you need to implement one more step. Visual Studio 2010/2008/2005 users don't need to implement the seventh step.
  7. Select your project and click on "Properties".

    Captcha image3
  8. Change Managed Pipeline mode Integrated to Classic.
  9. Now you are ready to implement the CAPTCHA feature in your application. Now open web.config and add the following httpHandlers code, under the <system.web> section:


     type="MSCaptcha.CaptchaImageHandler, MSCaptcha" />

  10. Open webform1.aspx and add the following line:



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

<%@ Register Assembly="MSCaptcha" Namespace="MSCaptcha" TagPrefix="cc1" %>

<!DOCTYPE html>


<html xmlns="">

<head runat="server">




    <form id="form1" runat="server">


    <cc1:CaptchaControl ID="cptCaptcha" runat="server"

        CaptchaBackgroundNoise="Low" CaptchaLength="5"

        CaptchaHeight="60" CaptchaWidth="200"

        CaptchaLineNoise="None" CaptchaMinTimeout="5"

        CaptchaMaxTimeout="240" FontColor = "#529E00" />



        <asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox>

        <br />

        <asp:Button ID="btnVerify" runat="server" Text="Verify Image" OnClick="btnVerify_Click" />

        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*Required" ControlToValidate = "txtCaptcha"></asp:RequiredFieldValidator>

        <br />

        <br />

        <asp:Label ID="lblErrorMessage" runat="server" Font-Names = "Arial" Text=""></asp:Label>






As you saw in the code above, cc1:CaptchaControl is a user control, that we get from "MSCaptha.dll".

Now when the user clicks on the button it will determine whether the text entered is valid or not.



protected void btnVerify_Click(object sender, EventArgs e)



        if (cptCaptcha.UserValidated)


            lblErrorMessage.ForeColor = System.Drawing.Color.Green;

            lblErrorMessage.Text = "Valid text";




            lblErrorMessage.ForeColor = System.Drawing.Color.Red;

            lblErrorMessage.Text = "InValid Text";



That's it. Press F5 and test your app.

Captcha image4

You can see in the preceding image, there's an image when you enter the same text that you get a valid message otherwise you will get an invalid error.

For any suggestions/queries, please send your comments. Happy Programming.

Up Next
    Ebook Download
    View all
    View all