In this Asp .Net tutorial we will learn how to create custom image CAPTCHA . Captcha is used to determine whether the user is human or bot. Captcha's are really very useful to stop spams and invalid data insertion by BOTS.
Step1: Create a new asp .net website.
Step2: Paste this code in your aspx page.
<%------ Label to show result wether we have passed/failed captcha.--%> |
| <%------ Image to hold captcha value -----%> | |
Step3: Add a new Geniric handler to your website and name it "CaptchaHandler.ashx".
Place this code in your Generic Handler.
public void ProcessRequest(HttpContext context)
{
string s = context.Request.QueryString.Get("txt");
context.Response.ContentType = "image/gif";
CreateImage(s).Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
}
private static Bitmap CreateImage(string sImageText)
{
Bitmap bmpImage = new Bitmap(1, 1);
int iWidth = 0;
int iHeight = 0;
Font MyFont = new Font("Arial", 18, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
Graphics MyGraphics = Graphics.FromImage(bmpImage);
iWidth = Convert.ToInt32(MyGraphics.MeasureString(sImageText, MyFont).Width) + 20;
iHeight = Convert.ToInt32(MyGraphics.MeasureString(sImageText, MyFont).Height) + 4;
bmpImage = new Bitmap(bmpImage, new Size(iWidth, iHeight));
MyGraphics = Graphics.FromImage(bmpImage);
MyGraphics.Clear(Color.Beige);
MyGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
MyGraphics.DrawString(sImageText, MyFont, new SolidBrush(Color.Brown), 10, 4);
MyGraphics.Flush();
return (bmpImage);
}
public bool IsReusable
{
get { return true; }
}
Place below Code in Default.aspx.cs Page.
Step4: CreateRandomString() Method.
This method is used to create a random string and pass to Generic handler. You can increase or decrease the text of captcha by increasing or decreasing value of "length" variable.
//---- Method to create random string to be used as captcha.
public void CreateRandomString(int length)
{
string guidResult = System.Guid.NewGuid().ToString();
guidResult = guidResult.Replace("-", string.Empty);
guidResult = guidResult.Substring(0, length);
imgCaptcha.ImageUrl = "~/CaptchaHandler.ashx?txt=" + guidResult;
Session["RandomImgText"] = guidResult;
}
Step5: ValidateForm() Method.
This method checks whether the input captcha is correct or not.
//--- Method to check whether entered captcha is correct or not.
protected bool ValidateForm()
{
bool IsValid = true;
if (txtImg.Text != Session["RandomImgText"].ToString())
{
IsValid = false;
}
return IsValid;
}
Step6: Page_Load() Method.
To generate captcha on page load.
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
//--- You can increase or decrease numbers
//--- to increase or decrease captcha strings.
CreateRandomString(6);
}
}
Step7: Button_Click() method.
protected void btnSubmit_Click(object sender, EventArgs e)
{
bool status = ValidateForm();
if (status == true)
{
lblCaptchaResult.Text = "You have successfully entered captcha";
}
else
{
lblCaptchaResult.Text = "Entered Text is incorrect";
}
//--- Recreate captcha.
CreateRandomString(6);
}
Final Output: