|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis 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 :
Generating Random Alpha-NumericsMicrosoft .NET framework provides The class RandomNumberGenerator rm;
rm = RandomNumberGenerator.Create();
The above code will create a byte[] data = new byte[3];
rm.GetNonZeroBytes(data);
The The generated random numbers will be stored in a 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 SkySo, now we got the random alphanumeric to display as a image. The image can be created by the 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.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||