0
Biulding Captcha image in C#.Net
Required Namespaces
1)using System.Drawing;
2)using System.Drawing.Text;
3)using System.Drawing.Imaging;
We need a seperate aspx page for create captcha image and we need to call that aspx page to verify the text displaying in that image.
Simple way of building captcha image and using is as shown below.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BuildCaptcha.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>
</div>
</form>
</body>
</html>
Code file C#.Net
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap objBMP = new Bitmap(60, 20);
Graphics objGraphics = Graphics.FromImage(objBMP);
objGraphics.Clear(Color.Wheat);
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
//' Configure font to use for text
Font objFont = new Font("Arial", 8, FontStyle.Italic);
string randomStr = "";
char[] myArray = new char[5];
int x;
//That is to create the random # and add it to our string
Random autoRand = new Random();
for (x = 0; x < 5; x++) { myArray[x] = System.Convert.ToChar(autoRand.Next(65,90)); randomStr += (myArray[x].ToString()); } //This is to add the string to session, to be compared later Session.Add("RandomStr", randomStr); //' Write out the text objGraphics.DrawString(randomStr, objFont, Brushes.Red, 3, 3); //' Set the content type and return the image Response.ContentType = "image/GIF"; objBMP.Save(Response.OutputStream, ImageFormat.Gif); objFont.Dispose(); objGraphics.Dispose(); objBMP.Dispose(); } } Calling Page <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default2" %>
<!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>
<img height="30" alt="" src="BuildCaptcha.aspx" width="80"/>
<asp:TextBox runat="Server" ID="txtCaptcha"/>
<asp:Button runat="Server" ID="btnSubmit" OnClick="btnSubmit_Click" Text="Submit" />
</div>
</form>
</body>
</html>
Calling page Code file C#.Net
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
//On “btnSubmit_Click” place the following code
if (Page.IsValid && (txtCaptcha.Text.ToString() == Session["RandomStr"].ToString()))
{
Response.Write("Code verification Successful");
}
else
{
Response.Write("Please enter info correctly");
}
}
}
1)using System.Drawing;
2)using System.Drawing.Text;
3)using System.Drawing.Imaging;
We need a seperate aspx page for create captcha image and we need to call that aspx page to verify the text displaying in that image.
Simple way of building captcha image and using is as shown below.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BuildCaptcha.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>
</div>
</form>
</body>
</html>
Code file C#.Net
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap objBMP = new Bitmap(60, 20);
Graphics objGraphics = Graphics.FromImage(objBMP);
objGraphics.Clear(Color.Wheat);
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
//' Configure font to use for text
Font objFont = new Font("Arial", 8, FontStyle.Italic);
string randomStr = "";
char[] myArray = new char[5];
int x;
//That is to create the random # and add it to our string
Random autoRand = new Random();
for (x = 0; x < 5; x++) { myArray[x] = System.Convert.ToChar(autoRand.Next(65,90)); randomStr += (myArray[x].ToString()); } //This is to add the string to session, to be compared later Session.Add("RandomStr", randomStr); //' Write out the text objGraphics.DrawString(randomStr, objFont, Brushes.Red, 3, 3); //' Set the content type and return the image Response.ContentType = "image/GIF"; objBMP.Save(Response.OutputStream, ImageFormat.Gif); objFont.Dispose(); objGraphics.Dispose(); objBMP.Dispose(); } } Calling Page <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default2" %>
<!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>
<img height="30" alt="" src="BuildCaptcha.aspx" width="80"/>
<asp:TextBox runat="Server" ID="txtCaptcha"/>
<asp:Button runat="Server" ID="btnSubmit" OnClick="btnSubmit_Click" Text="Submit" />
</div>
</form>
</body>
</html>
Calling page Code file C#.Net
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
//On “btnSubmit_Click” place the following code
if (Page.IsValid && (txtCaptcha.Text.ToString() == Session["RandomStr"].ToString()))
{
Response.Write("Code verification Successful");
}
else
{
Response.Write("Please enter info correctly");
}
}
}
More
Less