December 19, 2014 05:38 by
Peter
In this tutorial, I will explain you how to Develop a Captcha Image Generator in ASP.NET 5 with C# or VB.NET. CAPTCHA stands for "Completely Automated Public Turing test to tell Computers and Humans Apart". And here is the code that I use to generate take one .aspx page as GenerateCaptcha.aspx :
GenerateCaptcha.aspx.cs:
using System.IO;
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();
int height = 40;
int width = 110;
Bitmap bmp = new Bitmap(width, height);
RectangleF rectf = new RectangleF(12, 6, 0, 0);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawString(Session["captcha"].ToString(), new Font("Cambria", 13, FontStyle.Bold), Brushes.Red, rectf);
g.DrawRectangle(new Pen(Color.Green), 1, 1, width - 15, height -10);
g.Flush();
Response.ContentType = "image/jpeg";
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
g.Dispose();
bmp.Dispose();
}
}
Take .aspx where you want to use captcha.
UseCaptcha.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Create your own captcha image generator in ASP.NET using C#.NET/VB.NET</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table>
<tr>
<td style="height: 50px; width: 100px;">
<asp:Image ID="imgCaptcha" runat="server" />
</td>
<td valign="middle">
<asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="btnRefresh_Click" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
Enter Captcha:
<asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox><br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</div>
</form>
</body>
</html>
UseCaptcha.aspx.cs
using System.Text;
using System.Drawing.Imaging;
public partial class UseCaptcha : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillCaptcha();
}
}
void FillCaptcha()
{
try
Random random = new Random();
string combination = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
StringBuilder captcha = new StringBuilder();
for (int i = 0; i < 6; i++)
captcha.Append(combination[random.Next(combination.Length)]);
Session["captcha"] = captcha.ToString();
imgCaptcha.ImageUrl = "GenerateCaptcha.aspx";
}
catch
{
throw;
}
}
protected void btnRefresh_Click(object sender, EventArgs e)
{
FillCaptcha();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Session["captcha"].ToString() != txtCaptcha.Text)
Response.Write("Enter Valid Captcha Code");
FillCaptcha();
}
}
And this is the result from the code: