Click here to Skip to main content
12,450,977 members (54,439 online)
Click here to Skip to main content
Add your own
alternative version

Stats

61.6K views
891 downloads
32 bookmarked
Posted

Image Verification Helper Class

, 19 Jan 2005
Rate this:
Please Sign up or sign in to vote.
To generate random alphanumeric numbers in image format for image verification.

Sample image

Introduction

This article will help the intermediate ASP.NET developers who may be sometimes in need of implementing a feature like Image Verification when getting inputs from the users. Basically, this is a new technique which will stop the automated programs to process the input forms. But, here I'm not explaining you about the whole technique of Image Verification. This article will explain two things, which we really need :

  • How to generate alpha-numeric characters randomly?
  • How to make it as an image?

Generating Random Alpha-Numerics

Microsoft .NET framework provides System.Security.Cryptography namespace which contains RandomNumberGenerator to produce random numbers.

The class CImgVerify contains the code to produce the random alphabets/numbers in the function getRandomAlphaNumeric.

RandomNumberGenerator rm; 
rm = RandomNumberGenerator.Create();

The above code will create a RandomNumberGenerator object.

byte[] data = new byte[3]; 
rm.GetNonZeroBytes(data);

The byte type "data" will hold three elements of array to have the randomly generated numbers by GetNonZeroBytes. The GetNonZeroBytes fills an array of bytes with a cryptographically strong random sequence of nonzero values.

The generated random numbers will be stored in a byte variable as an array. Each and every elements will be an integer variable. So, make a proper alphanumeric string. The following code will use a loop to identify whether the randomly generated number falls into alphabets.

for(int nCnt=0;nCnt<=data.Length-1;nCnt++)
{ 
  //First convert it into a integer
  int nVal = Convert.ToInt32(data.GetValue(nCnt)); 
  // Check whether the converted int falls in between alphabets,symbols
  if(nVal > 32 && nVal < 127) 
  {
     sTmp = Convert.ToChar(nVal).ToString(); //Convert to character
  }
  else
  {
     sTmp = nVal.ToString(); //Remain as integer
  }
  sRand += sTmp.ToString(); //Append it to a string
}

Generating Image in the Sky

So, now we got the random alphanumeric to display as a image. The image can be created by the System.Drawing namespace. The following is the code which itself self explanatory.

public Bitmap generateImage(string sTextToImg)
{ // 
  //Here, i haven't used any try..catch 
  
  PixelFormat pxImagePattern = PixelFormat.Format32bppArgb; 
  Bitmap bmpImage = new Bitmap(1,1,pxImagePattern); 
  Font fntImageFont = new Font("Trebuchets",14); 
  Graphics gdImageGrp = Graphics.FromImage(bmpImage); 
  float iWidth = gdImageGrp.MeasureString(sTextToImg,fntImageFont).Width; 
  float iHeight = gdImageGrp.MeasureString(sTextToImg,fntImageFont).Height; 
  bmpImage = new Bitmap((int)iWidth,(int)iHeight,pxImagePattern ); 
  gdImageGrp = Graphics.FromImage(bmpImage); 
  gdImageGrp.Clear(Color.White); 
  gdImageGrp.TextRenderingHint = TextRenderingHint.AntiAlias; 
  gdImageGrp.DrawString(sTextToImg,fntImageFont, new SolidBrush(Color.Red),0,0); 
  gdImageGrp.Flush(); 
  return bmpImage;
}

If you have further queries/suggestions, post here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

vadivelkumar
Web Developer
India India
Vadivel Kumar is from India and he works in C#/ASP.NET platforms.


You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralImage Verification Pin
Jani0323-Jan-09 0:30
memberJani0323-Jan-09 0:30 
Generaldear vadivelkumar Pin
phuonglk4-Dec-08 1:52
memberphuonglk4-Dec-08 1:52 
QuestionCreating An Image While Making An Email Signup Form. Pin
mshariq29-Jun-06 21:26
membermshariq29-Jun-06 21:26 
GeneralRe: Creating An Image While Making An Email Signup Form. Pin
siravicb5-Mar-08 11:40
membersiravicb5-Mar-08 11:40 
GeneralRe: Creating An Image While Making An Email Signup Form. Pin
Mahmoud Aydeh18-Feb-09 6:05
memberMahmoud Aydeh18-Feb-09 6:05 
QuestionCan anybody provide me the function 'generateImage' in VB??? Pin
mohsindeveloper5-Dec-05 18:27
membermohsindeveloper5-Dec-05 18:27 
AnswerRe: Can anybody provide me the function 'generateImage' in VB??? Pin
zubairy29-Sep-11 11:54
memberzubairy29-Sep-11 11:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160826.1 | Last Updated 19 Jan 2005
Article Copyright 2005 by vadivelkumar
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid