Simple Captcha Using C# | CodeGuru

Simple Captcha Using C#

I was trying to find a solution to block spammers adding data to our database using C#. This is the quick solution I found. First create an image tag in the webform that has the submit form. <IMG height="30" alt="" src="Turing.aspx" width="80"> As you see the source for the picture will be a file called […]

Written By
CodeGuru Staff
CodeGuru Staff
Dec 16, 2012
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

I was trying to find a solution to block spammers adding data to our database using C#. This is the quick solution I found.

First create an image tag in the webform that has the submit form.

<IMG height="30" alt="" src="Turing.aspx" width="80">

As you see the source for the picture will be a file called "Turing.aspx".

Later create the webform "Turing.aspx" with the code below

public class Turing1 : System.Web.UI.Page
{

private void Page_Load(object sender, System.EventArgs e)
{

Bitmap objBMP =new System.Drawing.Bitmap(60,20);

Graphics objGraphics = System.Drawing.Graphics.FromImage(objBMP);

objGraphics.Clear(Color.Green);

objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;

//’ Configure font to use for text

Font objFont = new Font("Arial", 8, FontStyle.Bold);

string randomStr="";

int[] myIntArray = new int[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++)

{
myIntArray[x] = System.Convert.ToInt32 (autoRand.Next(0,9));

randomStr+= (myIntArray[x].ToString ());

}

//This is to add the string to session cookie, to be compared later

Session.Add("randomStr",randomStr);

//’ Write out the text

objGraphics.DrawString(randomStr, objFont, Brushes.White, 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();
}

}

And this is the code for the submit button on the main form

private void btnSubmit_ServerClick(object sender, System.EventArgs e)

{

if (Page.IsValid && (txtTuring.Value.ToString () ==Session["randomStr"].ToString ()))

{
// The Code to insert data
}
else

{

Label1.Text ="Please enter info correctly";

}
}

4) Finally make sure you add the related System libraries,

using System.Drawing;

using System.Drawing.Text;

using System.Drawing.Imaging;

in front of the class and this is it.

Hope this helps.

Regards

http://www.ottawatutors.com

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.