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.
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)
        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.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);
        Response.ContentType = "image/jpeg";
        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);

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

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)

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


    protected void btnRefresh_Click(object sender, EventArgs e)

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

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 All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.