How To Create Captcha Image With asp.net using c#

How To Create Captcha Image With asp.net using c#

In this article, you learn how to create captcha image in asp.net using c#

 

using System;  
using System.Collections.Generic;  
using System.Drawing;  
using System.Drawing.Drawing2D;  
using System.Drawing.Imaging;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
namespace Captcha  
{  
    public partial class Captcha: System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            Bitmap objBitmap = new Bitmap(131, 81);  
            Graphics objGraphics = Graphics.FromImage(objBitmap);  
            objGraphics.Clear(Color.White);  
            Random objRandom = new Random();  
            objGraphics.DrawLine(Pens.Black, objRandom.Next(0, 50), objRandom.Next(10, 30), objRandom.Next(0, 200), objRandom.Next(0, 50));  
            objGraphics.DrawRectangle(Pens.Blue, objRandom.Next(0, 20), objRandom.Next(0, 20), objRandom.Next(50, 80), objRandom.Next(0, 20));  
            objGraphics.DrawLine(Pens.Blue, objRandom.Next(0, 20), objRandom.Next(10, 50), objRandom.Next(100, 200), objRandom.Next(0, 80));  
            Brush objBrush = default (Brush); 
            HatchStyle[] aHatchStyles = new HatchStyle[]  
            {  
                HatchStyle.BackwardDiagonal, HatchStyle.Cross, HatchStyle.DashedDownwardDiagonal, HatchStyle.DashedHorizontal, 
                     HatchStyle.DashedUpwardDiagonal, HatchStyle.DashedVertical,  
                    HatchStyle.DiagonalBrick, HatchStyle.DiagonalCross, HatchStyle.Divot, HatchStyle.DottedDiamond, HatchStyle.DottedGrid, 
                    HatchStyle.ForwardDiagonal, HatchStyle.Horizontal,  
                    HatchStyle.HorizontalBrick, HatchStyle.LargeCheckerBoard, HatchStyle.LargeConfetti, HatchStyle.LargeGrid, HatchStyle.LightDownwardDiagonal, 
                   HatchStyle.LightHorizontal  
            };  
            RectangleF oRectangleF = new RectangleF(0, 0, 300, 300);  
            objBrush = new HatchBrush(aHatchStyles[objRandom.Next(aHatchStyles.Length - 3)], Color.FromArgb((objRandom.Next(100, 255)), (objRandom.Next(100, 
            255)), (objRandom.Next(100, 255))), Color.White);  
            objGraphics.FillRectangle(objBrush, oRectangleF);  
            string captchaText = string.Format("{0:X}", objRandom.Next(500000, 700000));
            Session["CaptchaVerify"] = captchaText.ToLower();  
            Font objFont = new Font("Courier New", 15, FontStyle.Bold); 
            objGraphics.DrawString(captchaText, objFont, Brushes.Black, 21, 21);  
            objBitmap.Save(Response.OutputStream, ImageFormat.Gif);  
        }  
    }  
}  

 

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Register.aspx.cs" Inherits="Captcha.Register" %>  
  
  
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">  
    <table>  
        <tr>  
            <td colspan="2">User Registration  
            </td>  
        </tr>  
        <tr>  
            <td>Full Name  
            </td>  
            <td>  
                <asp:TextBox runat="server" ID="txtFullName"></asp:TextBox>  
            </td>  
        </tr>  
        <tr>  
            <td>Email Id  
            </td>  
            <td>  
                <asp:TextBox runat="server" ID="txtEmail"></asp:TextBox>  
            </td>  
        </tr>  
        <tr>  
            <td>User Name  
            </td>  
            <td>  
                <asp:TextBox runat="server" ID="txtUserName"></asp:TextBox>  
            </td>  
        </tr>  
        <tr>  
            <td>Password  
            </td>  
            <td>  
                <asp:TextBox runat="server" ID="txtPassword" TextMode="Password"></asp:TextBox>  
            </td>  
        </tr>  
        <tr>  
            <td>Verification Code  
            </td>  
            <td>  
               <asp:Image ID="Image2" runat="server" Height="55px" ImageUrl="~/Captcha.aspx" Width="186px" />  
                <br />  
                <asp:Label runat="server" ID="lblCaptchaMessage"></asp:Label>  
          
            </td>  
        </tr>  
        <tr>  
            <td>Enter Verifaction Code  
            </td>  
            <td>  
                <asp:TextBox runat="server" ID="txtVerificationCode"></asp:TextBox>  
            </td>  
        </tr>  
        <tr>  
            <td colspan="2">  
                <asp:Button runat="server" ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" />  
            </td>  
        </tr>  
    </table>  
</asp:Content> 

 

Here is captcha is right otherwise it is wrong.

 

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
namespace Captcha 
{  
    public partial class Register: System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {}  
        protected void btnSubmit_Click(object sender, EventArgs e)  
        {  
            if (txtVerificationCode.Text.ToLower() == Session["CaptchaVerify"].ToString())  
            {  
                Response.Redirect("Default.aspx");  
            }  
            else  
            {  
                lblCaptchaMessage.Text = "Please enter correct captcha !!!!!";  
                lblCaptchaMessage.ForeColor = System.Drawing.Color.Red;  
            }  
        }  
    }  
}  

 

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Captcha._Default" %>  
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">  
        <br />  
        <asp:Label runat="server" ID="lblCaptchaMessage" ForeColor="Green">  
        </asp:Label>  
    </asp:Content>

 

 

using System;  
using System.Web.UI;  
namespace Captcha  
{  
    public partial class _Default: Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            lblCaptchaMessage.Text = "Success";  
            lblCaptchaMessage.ForeColor = System.Drawing.Color.Green;  
        }  
    }  
}  

2021-10-28