In this article I will explain with an example, how to use Mathematical Captcha control in ASP.Net using C# and VB.Net.
This article will explain, how to use ASPNET_Captcha in ASP.Net using C# and VB.Net.
Download ASPNET_Captcha Library
You need to download the free ASPNET_Captcha Library using the following link.
ASPNET_Captcha Implementation
Registering ASPNET_Captcha Control
In order to use ASPNET_Captcha control, you will need to add reference of ASPNET_Captcha Library and then register on the Page as shown below.
<%@Register TagPrefix="uc" Namespace="ASPNET_Captcha" Assembly="ASPNET_Captcha" %>
Adding ASPNET_Captcha Control to the page
You need to add the following HTML Markup on the page where you want to implement ASPNET_Captcha control.
<uc:ASPNET_Captcha ID="captcha1" runat="server" Align="Middle" Color="#FF0000" />
The ASPNET_Captcha control has been assigned with following properties:
Color – It is used to set the color of the Captcha Text. Default value is black.
Align – It is used to align the Captcha image along with text or other controls.
Web.Config Modifications
You will need to modify the Web.Config file as following shown Yellow in order to use the ASPNET_Captcha control.
        <compilation debug="true" targetFramework="4.5"/>
            <add verb="GET" path="Image.ashx" type="ASPNET_Captcha.Image, ASPNET_Captcha"/>
    <pages controlRenderingCompatibilityVersion="4.0"/>
        <validation validateIntegratedModeConfiguration="false"/>
            <add name ="Captcha" verb="*" path="Image.ashx" type="ASPNET_Captcha.Image, ASPNET_Captcha" resourceType="Unspecified"/>
HTML Markup
The following HTML markup consists of Captcha control, a TextBox, an ASP.Net RequiredFieldValidator, a Label and a Button control.
The Button has been assigned with an OnClick event handler.
The RequiredFieldValidator has been assigned with following property:
ControlToValidate – ID of the Control to be validated.
<%@Register TagPrefix="uc" Namespace="ASPNET_Captcha" Assembly="ASPNET_Captcha" %>
<!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""">
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <uc:ASPNET_Captcha ID="captcha1" runat="server" Align="Middle" Color="#FF0000" />
        <asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvCaptcha" runat="server" ErrorMessage="*Required"
            ControlToValidate="txtCaptcha" Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator>
        <asp:Label ID="lblMessage" runat="server"></asp:Label>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Submit" />
You will need to import the following namespace.
using System.Drawing;
Imports System.Drawing
Validating the Captcha
Following event handler is executed, when the Submit Button is clicked.
Inside this event handler, the TextBox value is validated using the Validate method and returns True or False.
Finally, based on the returned value, appropriate message is displayed in the Label control.
protected void Submit(object sender, EventArgs e)
    bool isValid = captcha1.Validate(txtCaptcha.Text.Trim());
    if (isValid)
        lblMessage.Text = "Valid!";
        lblMessage.ForeColor = Color.Green;
        lblMessage.Text = "Invalid!";
        lblMessage.ForeColor = Color.Red;
Protected Sub Submit(sender As Object, e As System.EventArgs)
    Dim isValid As Boolean = captcha1.Validate(txtCaptcha.Text.Trim)
    If isValid Then
        lblMessage.Text = "Valid!"
        lblMessage.ForeColor = Color.Green
        lblMessage.Text = "Invalid!"
        lblMessage.ForeColor = Color.Red
    End If
End Sub
Free ASP.Net Mathematical Captcha Control in VB.Net and C#