Friday, November 11, 2011

Create captcha

<%@ 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
Enter User Name :
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtUserName"
ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Enter Email Address :
</td>
<td>
<asp:TextBox ID="txtEmailAddress" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtEmailAddress"
ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Description :
</td>
<td>
<asp:TextBox ID="txtDescrition" TextMode="MultiLine" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Type Below Code :
</td>
<td>
<asp:TextBox ID="txtImage" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtImage"
ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<!– Get Image From the GenerateImage Page –>
<img alt="" id="img" src="GenerateImage.aspx" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" />
<asp:Button ID="btnCancel" Text="Cancel" runat="server" OnClick="btnCancel_Click"
ValidationGroup="Cancel" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>


Then .cs page


using System;
using System.Collections;
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 Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{

string str = Session["ImageString"].ToString();
if (str == txtImage.Text)
{
Response.Write("you enter right");
}
else
{
Response.Write("you enter false");
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("default.aspx");
}
}



Create a page name it as genrateImage.aspx


in genrateimage.cs page create a class like this


using System;
using System.Collections;
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;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public partial class GenerateImage : System.Web.UI.Page
{
private int _height = 75;
public int height
{
get { return _height; }
set { _height = value; }
}

private string _text = "generateimageh";
public string text
{
get { return _text; }
set { _text = value; }
}

private int _width = 275;
public int width
{
get { return _width; }
set { _width = value; }
}
///

/// Get Character from Random Number
///

///
///
public string getChar(int genNo)
{
switch (genNo)
{
case 0:
return "a";
case 1:
return "b";
case 2:
return "c";
case 3:
return "d";
case 4:
return "e";
case 5:
return "f";
case 6:
return "g";
case 7:
return "h";
case 8:
return "i";
case 9:
return "j";
}
return string.Empty;
}

//generating random numbers.
private Random ObjRandom = new Random();

protected void Page_Load(object sender, EventArgs e)
{
//generate random text
Random r = new Random();
string str = r.Next().ToString().Substring(0, 4);
char[] ch = str.ToCharArray();
text = string.Empty;
foreach (char var in ch)
{
//get appropriate char
text += getChar(Convert.ToInt32(var.ToString()));
}
//generate random image
GenerateImagew();
}


///

/// Generate Image
///

private void GenerateImagew()
{
//using System.Drawing; and using System.Drawing.Imaging;
//Create a new 32-bit bitmap image.
//specify height width
//if you want to change pass that value in to query string
Bitmap ObjBitmap = new Bitmap(this.width, this.height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//Create a graphics object
Graphics ObjGraphic = Graphics.FromImage(ObjBitmap);
ObjGraphic.SmoothingMode = SmoothingMode.HighQuality;

Rectangle ObjRect = new Rectangle(0, 0, this.width, this.height);
// Fill in the background color
//using System.Drawing.Drawing2D;
//you specify different fillup style
HatchBrush ObjHatchBrush = new HatchBrush(HatchStyle.BackwardDiagonal, Color.Transparent, Color.Transparent);
ObjGraphic.FillRectangle(ObjHatchBrush, ObjRect);
// Text Font Size
SizeF ObjectFontSize;
float fontSize = ObjRect.Height + 3;
Font ObjFont;
// Adjust the font size until the text fits within the image.
do
{
fontSize--;
ObjFont = new Font(FontFamily.GenericSerif, fontSize, FontStyle.Bold);
ObjectFontSize = ObjGraphic.MeasureString(this.text, ObjFont);
} while (ObjectFontSize.Width > ObjRect.Width);

// Set up the text format.
StringFormat ObjectStringFormat = new StringFormat();
ObjectStringFormat.Alignment = StringAlignment.Center;
ObjectStringFormat.LineAlignment = StringAlignment.Center;

// Create a path using the text and warp it randomly.
GraphicsPath ObjGraphicPath = new GraphicsPath();
ObjGraphicPath.AddString(this.text, ObjFont.FontFamily, (int)ObjFont.Style, ObjFont.Size, ObjRect, ObjectStringFormat);
float size = 6F;
PointF[] points =
{
new PointF(this.ObjRandom.Next(ObjRect.Width) / size, this.ObjRandom.Next(ObjRect.Height) / size),
new PointF(ObjRect.Width - this.ObjRandom.Next(ObjRect.Width) / size, this.ObjRandom.Next(ObjRect.Height) / size),
new PointF(this.ObjRandom.Next(ObjRect.Width) / size, ObjRect.Height - this.ObjRandom.Next(ObjRect.Height) / size),
new PointF(ObjRect.Width - this.ObjRandom.Next(ObjRect.Width) / size, ObjRect.Height - this.ObjRandom.Next(ObjRect.Height) / size)
};
Matrix ObjMatrix = new Matrix();
ObjMatrix.Translate(0F, 0F);
ObjGraphicPath.Warp(points, ObjRect, ObjMatrix, WarpMode.Perspective, 0F);

//Draw Text
ObjHatchBrush = new HatchBrush(HatchStyle.Wave, Color.Gray, Color.DarkGray);
ObjGraphic.FillPath(ObjHatchBrush, ObjGraphicPath);
//Add more noise in the image
int m = Math.Max(ObjRect.Width, ObjRect.Height);
for (int i = 0; i < (int)(ObjRect.Width * ObjRect.Height / 30F); i++)
{
int x = this.ObjRandom.Next(ObjRect.Width);
int y = this.ObjRandom.Next(ObjRect.Height);
int w = this.ObjRandom.Next(m / 52);
int h = this.ObjRandom.Next(m / 52);
ObjGraphic.FillEllipse(ObjHatchBrush, x, y, w, h);
}
ObjFont.Dispose();
ObjHatchBrush.Dispose();
ObjGraphic.Dispose();
this.Response.ContentType = "image/jpeg";
Session.Add("ImageString", this.text);
ObjBitmap.Save(this.Response.OutputStream, ImageFormat.Jpeg);
}
}

No comments:

Post a Comment