Wednesday, 29 June 2011

                                   use of    image captcha







Verification Code  sows in textbox




make


 default.aspx like this 
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Simple Captcha Example</title>
    <style type="text/css">
        td
            {
                font-family:Tahoma;
                font-size:11px;
                background-color:#EEEEEE;
            }
            
        span
            {
                font-family:Tahoma;
                font-size:14px;  
                color:Red;         
            }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width="500">
            <tr>
                <td colspan="3">
                    <span></span><asp:Label ID="lblMessage" runat="server"></asp:Label></span>
                </td>
            </tr>
            <tr>
                <td>Image Code</td>
                <td colspan="2"><img src="Captcha.aspx" alt="Verification Code" /></td>
            </tr>
            <tr>
                <td>Enter Code</td>
                <td><asp:TextBox ID="txtCode" runat="server"></asp:TextBox></td>
                <td><asp:Button ID="btnVerify" runat="server" onclick="btnVerify_Click" 
                        Text="Verify" /></td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

 and do the following coding in .aspx

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{

    protected void btnVerify_Click(object sender, EventArgs e)
    {
        string user_code = txtCode.Text;
        string captcha_code = Session["CAPTCHA_CODE"].ToString();

        if (user_code != captcha_code)
        {
            lblMessage.Text = "Verification Failed.";
        }
        else
        {
            lblMessage.Text = "Verification Successful";
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
now take another form named captcha and set html like this,

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

and do the following code ib capcha .aspx .cs like this,

using System;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
using System.Drawing.Drawing2D;


public partial class Captcha : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strString = "abcdefghijklmnopqrstuvwxyz0123456789";

        Bitmap image = new Bitmap(Server.MapPath("image.png"));
        Graphics g = Graphics.FromImage(image);
        Rectangle area = new Rectangle(0,0,image.Width-1, image.Height-1);
        Pen pen = new Pen(Color.SkyBlue);

        LinearGradientBrush lgBrush = new LinearGradientBrush(area, Color.White, Color.FromArgb(220,220,220), LinearGradientMode.Vertical);
        
        g.FillRectangle(lgBrush, area);
        g.DrawRectangle(pen, area);

        SolidBrush brush = new SolidBrush(Color.Black);
        Random random = new Random();
        int randomCharIndex = 0;
        char randomChar;
        int charPosition = 10;

        string captcha = "";
        for (int i = 0; i < 7; i++)
        {
            randomCharIndex = random.Next(0, strString.Length);
            randomChar = strString[randomCharIndex];
            g.DrawString(Convert.ToString(randomChar), new Font("arial", random.Next(16,25)), brush, charPosition, 10);
            charPosition += 17;
            captcha += Convert.ToString(randomChar);
        }
        Session["CAPTCHA_CODE"] = captcha;
        Response.ContentType = "image/jpeg";
        image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
}

and take a folder name image and paste a i mage now code ready....



No comments:

Post a Comment