German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Generate an Image from Rendom Text (Creating CAPTHCA) with .NET

Written by omerkamal on Mar 07, 2007
Text to image generator to block bots submitting to your database using ASP.Net

Explanation:

We were trying to find a solution to block bots adding data to your database using C#. There are many spammers around now days trying to add advertisements. This can also be dangerous for your database if the data was malicious. This is the quick solution I found. 

This Article and the Code is really easy to understand and self explainatry. If still you want some more Explanation please use the Forum or just leave a Comment.
 
1) First create an image tag in the webform that has the submit form. As you see the source for the picture will be a file called “Turing.aspx”
 
2) 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 ());
                  }
                   //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();

            }
}


3) 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";
                  }
}

Visitors/Readers Comments
(for questions please use The Forum)



Pallavi

How to display random number as an image

 

11/09/2007 07:11:43 UTC

Naison
Good work mate. Simple and easy to use.

Naison Garvasis Pekkatil

09/06/2008 03:57:45 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)