How to generate own captcha without DLL in asp.net c#

In this article, we are doing the create dynamically own captcha without any DLL. In this market we have so many 3rd party tools for making the captcha but on that we do have any right to design that and making own style.
In this post, we have created a captchagenerator.aspx page for designing of captcha and reusability of code , he can create a this page and we can use as our requirement


Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="captchagenerator.aspx.cs" Inherits="captchagenerator" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>


Code behind(c#):-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

public partial class captchagenerator : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();
        int height = 30;
        int width = 100;
        Bitmap bmp = new Bitmap(width, height);

        RectangleF rectf = new RectangleF(10, 5, 0, 0);

        Graphics grph = Graphics.FromImage(bmp);
        grph.Clear(Color.White);
        grph.SmoothingMode = SmoothingMode.AntiAlias;
        grph.InterpolationMode = InterpolationMode.HighQualityBicubic;
        grph.PixelOffsetMode = PixelOffsetMode.HighQuality;
        grph.DrawString(Session["captchabox"].ToString(), new Font("Thaoma", 12, FontStyle.Italic), Brushes.Red, rectf);
        grph.DrawRectangle(new Pen(Color.Blue), 1, 1, width - 2, height - 2);
        grph.Flush();
        Response.ContentType = "image/jpeg";
        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
        grph.Dispose();
        bmp.Dispose();
    }
}


Source code :-

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
   <div align="center">
    <fieldset style ="width:200px;">
    <legend>Registration</legend>
        <asp:TextBox ID="txtname" placeholder="Name" runat="server"
            Width="180px"></asp:TextBox>
        <br />
        <br />
        <asp:TextBox ID="txtgender" placeholder="Gender" runat="server"
            Width="180px" ></asp:TextBox>
        <br />
        <br />

        <asp:TextBox ID="txtlocation" placeholder="Current Location" runat="server"
            Width="180px" ></asp:TextBox>
                   <br />
        <br />
        <asp:TextBox ID="txtcaptcha" placeholder="Enter Below Captcha" runat="server"
            Width="180px" ></asp:TextBox>
                   <br />
        <br />

        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                        <ContentTemplate>
                           
                                        <asp:Image ID="imgCaptcha" runat="server" align="absmiddle" />
                                  
                                        &nbsp;&nbsp;
                                  
                                        <asp:Button ID="btnRefresh" runat="server" Text="Refresh"
                                           onclick="btnRefresh_Click" />
                              
                        </ContentTemplate>
                    </asp:UpdatePanel>
                    <br />
                    <br />
        <asp:Button ID="btnsubmit" runat="server" Text="Submit"
           Width="81px" onclick="btnsubmit_Click" />
            <br />
    </form>
</body>
</html>



Code behind(c#):-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class empregistration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindcaptcha();
        }
    }
    protected void bindcaptcha()
    {
      
            Random random = new Random();
            string combination = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            StringBuilder captcha = new StringBuilder();
            for (int i = 0; i < 7; i++)
            captcha.Append(combination[random.Next(combination.Length)]);
            Session["captchabox"] = captcha.ToString();
            imgCaptcha.ImageUrl = "captchagenerator.aspx?" + DateTime.Now.Ticks.ToString();
       
    }

    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        if (Session["captchabox"].ToString() != txtcaptcha.Text)
        {
            Response.Write("<script>alert('Invalid Captcha...')</script");

           
        }
        else
        {
            Response.Write("<script>alert('Valid Captcha...')</script");
        }
        bindcaptcha();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        bindcaptcha();
    }
}

Out-Put:-



For More:- 
How to generate own captcha without DLL in asp.net c# How to generate own captcha without DLL in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 2:27:00 PM Rating: 5

No comments:

Powered by Blogger.