Captcha Code
In Asp.Net Using C#
So for
this current article first you needed to create a new asp.net application. in
this add two aspx file first default.aspx and second one is captchacode.aspx .
In your captchacode.aspx add the below code . This code is used for making the captcha code.
In your captchacode.aspx add the below code . This code is used for making the captcha code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.IO;
namespace Cappachacode
{
public partial class captcha : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Random r = new Random();
//Captcha no
string captchacode = r.Next(10000, 99999).ToString();
//Storing captcha no in sessin to validate
Session["captchacode"] = captchacode;
Color FontColor = Color.Blue;
Color BackColor = Color.White;
//Define fone name
String FontName = "Arial";
//Define the font size
int FontSize = 40;
//define captcha height
int Height = 60;
//Define captcha width
int Width = 174;
//Define bit map image width and height
Bitmap bitmap = new Bitmap(Width, Height);
Graphics graphics = Graphics.FromImage(bitmap);
Color color = Color.Gray; ;
Font font = new Font(FontName, FontSize);
//define where the text will be displayed in the specified area of the image
PointF point = new PointF(5.0F, 5.0F);
//Assigning fone color
SolidBrush BrushForeColor = new SolidBrush(FontColor);
//Define captcha background color
SolidBrush BrushBackColor = new SolidBrush(BackColor);
Pen BorderPen = new Pen(color);
Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1));
graphics.FillRectangle(BrushBackColor, displayRectangle);
graphics.DrawRectangle(BorderPen, displayRectangle);
//Define string format
StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);
StringFormat format2 = new StringFormat(format1);
//Draw text string using the text format
graphics.DrawString(captchacode, font, Brushes.Red, (RectangleF)displayRectangle, format2);
Response.ContentType = "image/jpeg";
//Defining bitmap type
bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.IO;
namespace Cappachacode
{
public partial class captcha : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Random r = new Random();
//Captcha no
string captchacode = r.Next(10000, 99999).ToString();
//Storing captcha no in sessin to validate
Session["captchacode"] = captchacode;
Color FontColor = Color.Blue;
Color BackColor = Color.White;
//Define fone name
String FontName = "Arial";
//Define the font size
int FontSize = 40;
//define captcha height
int Height = 60;
//Define captcha width
int Width = 174;
//Define bit map image width and height
Bitmap bitmap = new Bitmap(Width, Height);
Graphics graphics = Graphics.FromImage(bitmap);
Color color = Color.Gray; ;
Font font = new Font(FontName, FontSize);
//define where the text will be displayed in the specified area of the image
PointF point = new PointF(5.0F, 5.0F);
//Assigning fone color
SolidBrush BrushForeColor = new SolidBrush(FontColor);
//Define captcha background color
SolidBrush BrushBackColor = new SolidBrush(BackColor);
Pen BorderPen = new Pen(color);
Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1));
graphics.FillRectangle(BrushBackColor, displayRectangle);
graphics.DrawRectangle(BorderPen, displayRectangle);
//Define string format
StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);
StringFormat format2 = new StringFormat(format1);
//Draw text string using the text format
graphics.DrawString(captchacode, font, Brushes.Red, (RectangleF)displayRectangle, format2);
Response.ContentType = "image/jpeg";
//Defining bitmap type
bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
}
}
}
Here i have stored the random no in session. this session value we will validate with the user entered value.
your .aspx page will look as shown below.
<%@ Page
Language="C#" AutoEventWireup="true"
CodeBehind="captcha.aspx.cs"
Inherits="Cappachacode.captcha" %>
Now come your default.aspx page . in this page add a text box and a button anda image control.
In this image url will be the the path of the image.
<%@ Page
Language="C#" AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs"
Inherits="Cappachacode.WebForm1" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="lblmessage" runat="server"
style="color: #FFFFFF; font-weight: 700; background-color: #FF0000"></asp:Label>
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server" Width="200px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Validate Captcha" />
<br />
<br />
<asp:Image ID="Image1" runat="server" Height="40px" ImageUrl="captcha.aspx"
Width="149px" />
</form>
</body>
</html>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="lblmessage" runat="server"
style="color: #FFFFFF; font-weight: 700; background-color: #FF0000"></asp:Label>
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server" Width="200px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Validate Captcha" />
<br />
<br />
<asp:Image ID="Image1" runat="server" Height="40px" ImageUrl="captcha.aspx"
Width="149px" />
</form>
</body>
</html>
Now generate the button click event of the button and add the below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Cappachacode
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string captchacode=Session["captchacode"].ToString();
if (captchacode == TextBox1.Text)
{
Response.Redirect("WebForm2.aspx");
}
else
{
lblmessage.Text = "You have entered WRONG captcha code.";
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Cappachacode
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string captchacode=Session["captchacode"].ToString();
if (captchacode == TextBox1.Text)
{
Response.Redirect("WebForm2.aspx");
}
else
{
lblmessage.Text = "You have entered WRONG captcha code.";
}
}
}
}
now run the application.
Now click the button
Now again run the page and enter wrong code.
_____________________________________
DOWNLOAD
_____________________________________
No comments:
Post a Comment