Thursday, 10 April 2014

Create your own captcha image generator in ASP.NET using C#.NET/VB.NET

Hi friends,in this article i will explain about Create your own captcha image generator in ASP.NET using C#.NET/VB.NET.
In previous articles i explained about How to Create Simple RDLC Report in ASP.NET,Create ,list and Alter the trigger associated with a table with SQL Server? and Binding and Sorting Grid in ASP.NET MVC using Jquery.
Generating captcha take one .aspx page as GenerateCaptcha.aspx and write the following code.
GenerateCaptcha.aspx.cs:
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public partial class GenerateCaptcha : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();
        int height = 40;
        int width = 110;
        Bitmap bmp = new Bitmap(width, height);
        RectangleF rectf = new RectangleF(12, 6, 0, 0);
        Graphics g = Graphics.FromImage(bmp);
        g.Clear(Color.White);
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        g.DrawString(Session["captcha"].ToString(), new Font("Cambria", 13, FontStyle.Bold), Brushes.Red, rectf);
        g.DrawRectangle(new Pen(Color.Green), 1, 1, width - 15, height -10);
        g.Flush();
        Response.ContentType = "image/jpeg";
        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
        g.Dispose();
        bmp.Dispose();
    }
}



Take .aspx where you want to use captcha.
UseCaptcha.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Create your own captcha image generator in ASP.NET using C#.NET/VB.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <table>
                    <tr>
                        <td style="height: 50px; width: 100px;">
                            <asp:Image ID="imgCaptcha" runat="server" />
                        </td>
                        <td valign="middle">
                            <asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="btnRefresh_Click" />
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:UpdatePanel>
        Enter Captcha:
        <asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox><br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
    </div>
    </form>
</body>
</html>

UseCaptcha.aspx.cs
using System.Text;
using System.Drawing.Imaging;

public partial class UseCaptcha : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillCaptcha();
        }
    }

    void FillCaptcha()
    {
        try
        {
            Random random = new Random();
            string combination = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            StringBuilder captcha = new StringBuilder();
            for (int i = 0; i < 6; i++)
                captcha.Append(combination[random.Next(combination.Length)]);
            Session["captcha"] = captcha.ToString();
            imgCaptcha.ImageUrl = "GenerateCaptcha.aspx";
        }
        catch
        {

            throw;
        }
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        FillCaptcha();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Session["captcha"].ToString() != txtCaptcha.Text)
            Response.Write("Enter Valid Captcha Code");      
        FillCaptcha();
    }
}



The output of the above page as shown in the below figure.
You can download the code by clicking on the below Download image.

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.