Minifycode 2021-10-28 Viewed 1.1K times ASP.NET

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;  
        }  
    }  
}  

How To Create Captcha Image With asp.net using c#
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html> <head> <title>Asp.Net Repeater Control</title> </head> <body> <form id="form1" runat="server"> <div style="font:13px Verdana;width:310px;"> <asp:ScriptManager ID="scriptManager" runat="server" EnablePageMethods="true" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <h3>Asp.Net Repeater Control</h3> <%--THE REPEATER CONTROL.--%> <asp:Repeater id="rp" runat="server"> <%--HEADER OF THE REPEATER--%> <HeaderTemplate> <table border="0" width="200px"> </HeaderTemplate> <%--SHOWING ITEMS--%> <ItemTemplate> <tr> <td style="padding:2px; border:solid 1px #CCC;"> <asp:Label Text='<%# Container.DataItem.ToString() %>' runat="server"></asp:Label> </td> </tr> </ItemTemplate> <%--ALTERNATE TEMPLATE (SHOWING ITEMS IN DIFFERENT COLOR--%> <AlternatingItemTemplate> <tr> <td style="padding:2px; border:solid 1px #CCC; background:#EAF7FB; width:200px;"> <asp:Label Text='<%# Container.DataItem.ToString() %>' runat="server"></asp:Label> </td> </tr> </AlternatingItemTemplate> <%--REPEATER FOOTER--%> <FooterTemplate> </table> <div style="padding:20px 0;"> <asp:Label ID="lblFoot" runat="server"></asp:Label> </div> </FooterTemplate> </asp:Repeater> <%--BUTTON CONTROL--%> <asp:Button ID="btVF" Text="View Files" AutoPostBack="true" OnClick="Show" Font-Names="sanserif" Font-Italic="true" Font-Size="155%" runat="server" /> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="PostBackTrigger" /> </Triggers> </asp:UpdatePanel> </div> </form> </body> </html> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {} protected void Show(object sender, EventArgs e) { DirectoryInfo Folder = new DirectoryInfo(Server.MapPath("Files\\")); FileInfo[] fileList = Folder.GetFiles("*.*"); //BIND THE FILE LIST WITH THE REPEATER CONTROL. rp.DataSource = fileList; rp.DataBind(); } } Adding the <AlternatingTemplate> below the <ItemTemplate> will show files with a different background color for every alternate row. <AlternatingItemTemplate> <div style="padding:3px; border:solid 1px #FFF; background:#black"> <asp:Label Text='<%# Container.DataItem.ToString() %>' runat="server"></asp:Label> </div> </AlternatingItemTemplate> </AlternatingItemTemplate> <SeparatorTemplate><hr /> </SeparatorTemplate> Add the <HeaderTemplate> element just above the <ItemTemplate> <HeaderTemplate> <div style="padding-bottom:20px"><strong>Files</strong> (Header)</div> </HeaderTemplate> Repeater with <FooterTemplate> element <FooterTemplate> <div style="padding-top:20px"><asp:Label ID="lblFooter" runat="server"></asp:Label></div> </FooterTemplate> Code Behind In C# Control ft = rp.Controls[rep.Controls.Count - 1].Controls[0]; Label lbl_Footer = ft.FindControl("lblFooter") as Label; lbl_Footer.Text = "Showing total <b>" + fileList.Length + "</b> files in the Footer.";