Create Captcha with Refresh Button in Asp.Net
Here I will explain
how to create captcha in asp.net using c# with refresh button
First we take a page which name is GenerateCaptcha.aspx
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="GenerateCaptcha.aspx.cs"
Inherits="GenerateCaptcha"
%>
<!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">
<div>
</div>
</form>
</body>
</html>
Code with GenerateCaptcha.aspx.cs………………………
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//using this namespace
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public partial class GenerateCaptcha
: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs
e)
{
Response.Clear();
// Initializes a new instance of the System.Drawing.Bitmap class
with the specified size (width,height).
Bitmap bmp = new Bitmap(100, 30);
RectangleF rec = new RectangleF(15, 7, 0, 0);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
g.SmoothingMode
= SmoothingMode.AntiAlias;
g.InterpolationMode
= InterpolationMode.HighQualityBicubic;
//Gets or set a value specifying how pixels are offset during
rendering of this System.Drawing.Graphics.
g.PixelOffsetMode
= PixelOffsetMode.HighQuality;
// Draws the specified text
string in the specified rectangle with the specified System.Drawing.Brush and
System.Drawing.Font objects.
g.DrawString(Session["captcha"].ToString(), new Font("Verdana", 12, FontStyle.Regular),
Brushes.Red, rec);
// Draws a rectangle specified by a coordinate pair, a width, and
a height (pen,x-coordinate,y-coordinate,width,height)
g.DrawRectangle(new Pen(Color.Green), 1, 1, 98, 28);
// Forces execution of all
pending graphics operations and returns immediately without waiting for the
operations to finish.
g.Flush();
Response.ContentType
= "image/jpeg";
bmp.Save(Response.OutputStream,
ImageFormat.Jpeg);
// Releases all resources used by this System.Drawing.Graphics
with Dispose.
g.Dispose();
bmp.Dispose();
}
}
We take another web page which name is Registration.aspx.cs
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Registration.aspx.cs"
Inherits="Registration"
%>
<!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">
<table style="border: solid 1px black; padding: 2px; position: relative; top: 10px;" align="center">
<tr><td colspan="2">
<asp:ScriptManager ID="ScriptManager1"
runat="server"></asp:ScriptManager></td></tr>
<tr><td colspan="2" align="center"><b>Captcha
Concept in Asp.Net</b></td></tr>
<tr><td>Name</td><td><asp:TextBox ID="txtname" runat="server" ></asp:TextBox></td></tr>
<tr><td>EmailId</td><td><asp:TextBox ID="txtemailid" runat="server" ></asp:TextBox></td></tr>
<tr><td>Password</td><td><asp:TextBox ID="txtpassword" runat="server"
TextMode="Password" ></asp:TextBox></td></tr>
TextMode="Password" ></asp:TextBox></td></tr>
<tr><td>Confirm Password</td><td><asp:TextBox ID="txtcpassword" runat="server"
TextMode="Password" ></asp:TextBox></td></tr>
TextMode="Password" ></asp:TextBox></td></tr>
<tr><td>Enter Below Code :</td><td><asp:TextBox ID="txtcaptcha" runat="server" ></asp:TextBox></td></tr>
<tr><td></td><td valign="middle">
<asp:UpdatePanel ID="updatepanel1"
runat="server"
ChildrenAsTriggers="False"
UpdateMode="Conditional">
<ContentTemplate>
<table>
<tr><td style="height: 40px; width:100px;"><asp:Image ID="imgCaptcha" runat="server" /></td>
<td valign="middle"><asp:LinkButton ID="linkbutton" runat="server" onclick="LinkRefresh_Click" >Refresh</asp:LinkButton></td></tr>
</table>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="linkbutton"
/>
</Triggers>
</asp:UpdatePanel>
</td></tr>
<tr><td align="center"
colspan="2">
<asp:Button ID="btnsave"
runat="server"
Text="Register"
OnClick="btnRegister_Click"
/></td></tr>
<tr><td colspan="2" valign="middle"><asp:Label ID="lblmessage" runat="server" Visible="false"></asp:Label></td></tr>
</table>
</form>
</body>
</html>
Code with Registration.aspx.cs………………………
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Drawing;
public partial class Registration
: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs
e)
{
if(!IsPostBack)
FillCapctha();
}
void FillCapctha()
{
try
{
Random random = new Random();
string combination = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
StringBuilder captcha = new StringBuilder();
//no 7= no of letter
show in Captcha Box
for (int i = 0; i
< 7; i++)
captcha.Append(combination[random.Next(combination.Length)]);
Session["captcha"] = captcha.ToString();
imgCaptcha.ImageUrl
= "GenerateCaptcha.aspx?" + DateTime.Now.Ticks.ToString();
}
catch(Exception
ex)
{
lblmessage.Visible
= true;
lblmessage.ForeColor
= Color.Red;
lblmessage.Text
=ex.Message;
}
}
protected void
btnRegister_Click(object sender, EventArgs e)
{
if (Session["captcha"].ToString()
!= txtcaptcha.Text)
{
lblmessage.Visible
= true;
lblmessage.ForeColor
= Color.Red;
lblmessage.Text
= "Invalid Captcha Code";
}
else
{
lblmessage.Visible
= true;
lblmessage.ForeColor
= Color.Green;
lblmessage.Text
= "Valid Captcha Code";
}
FillCapctha();
}
protected void
LinkRefresh_Click(object sender, EventArgs e)
{
FillCapctha();
}
thnku so much sirji... i need it...thnx... it is very very useful nowadays...
ReplyDeleteThe 2016 Rolex Replica Watches are already in the Swiss Rolex Watches designing studio and the Replica Rolex Watches production is staring very soon. Replica Hermes Handbags handbagmakers are working now on the new 2016 Replica Louis Vuitton Handbags projects.
ReplyDelete