How To create Captacha Image in C#

How to create Captacha 

Captacha is One Type of Generated random number and alphabetic  image  then how to generate
first of all add using namespace for bitmap 
 using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Drawing;

First of Write code in Page_load Event to create Bitmap image

 Bitmap objBMP = new Bitmap(180, 51);
        Graphics objGraphics = Graphics.FromImage(objBMP);
        objGraphics.Clear(Color.OrangeRed);
        objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
        Font objFont = new Font("arial", 30, FontStyle.Regular);
        string randomStr = GeneratePassword();
        Session.Add("randomStr", randomStr);
        objGraphics.DrawString(randomStr, objFont, Brushes.White, 2, 2);
        //Response.ContentType = "image/GIF";
        //Response.ContentType = "image/GIF";
        objBMP.Save(Response.OutputStream, ImageFormat.Gif);
        objFont.Dispose();
        objGraphics.Dispose();
        objBMP.Dispose();

In this Generate Bitmap image one Function GeneratePassword is generate image and return string
value to page_load.

   public string GeneratePassword()
    {
        string allowedChars = "a,b,c,d,e,f,g,h,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,";
        allowedChars += "A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z,";
        allowedChars += "2,3,4,5,6,7,8,9";
        char[] sep ={ ',' };
        string[] arr = allowedChars.Split(sep);
        string passwordString = "";
        string temp;
        Random rand = new Random();
        for (int i = 0; i < 6; i++)
        {
            temp = arr[rand.Next(0, arr.Length)];
            passwordString += temp;
        }
        return passwordString;
    }

 <table border="1" width="70%">
                <tr>
                    <td align="center" colspan="2">
                        Registration of Captcha Details
                    </td>
                </tr>
                <tr>
                    <td>
                        Captcha
                    </td>
                    <td>
                        <asp:Image ID="imgresult" runat="server" />
                    </td>
                </tr>
            </table>
            <table border="1" width="70%">
                <tr>
                    <td>
                        Captcha Match
                    </td>
                    <td>
                        <asp:TextBox ID="txtcaptcha" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="btnsubmit_Click" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <asp:Label ID="lblmessage" runat="server"></asp:Label>
                    </td>
                </tr>
            </table>

And Last button click then write code for compare capatcha image and textbox write value and Show error label

  string randomStr = GeneratePassword();
        int result = string.Compare(randomStr, txtcaptcha.Text);
        if (result == 0)
        {
            lblmessage.Text = "Captcha Value Match Successfully.";
        }
        else if (result == -1)
        {
            lblmessage.Text = "Test String1 is less than Test String2";

        }
        else if (result == 1)
        {
            lblmessage.Text = "Test String1 is greater than Test String2";

        }


Ebook Download
View all
Learn
View all