Monday, July 5, 2010

How to generate captcha in C#

The code below generates randomly captcha. You can access it in another page using session in C#.

Add using System.IO
the code is :

protected void Page_Load(object sender, EventArgs e)
{
System.Drawing.Bitmap objBmp = new System.Drawing.Bitmap(90, 25); System.Drawing.Graphics objGraphics = System.Drawing.Graphics.FromImage(objBmp); objGraphics.Clear(System.Drawing.Color.Green); objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; System.Drawing.Font objFont = new System.Drawing.Font("Acoustic Light", 11, System.Drawing.FontStyle.Bold);
string strRandom = "";
string[] strArray = new string[36];
strArray = new string[] { "a", "b", "c" , "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","A","R","S","F"};
Random autoRand = new Random();
int x;
for (x = 0; x < i =" Convert.ToInt32(autoRand.Next(0,">
}
Session.Add("strRandom", strRandom);
objGraphics.DrawString(strRandom, objFont, System.Drawing.Brushes.White, 3, 3); Response.ContentType = "image/GIF";
objBmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif); objFont.Dispose(); objGraphics.Dispose(); objBmp.Dispose();
}

Enjoy C#


No comments:

Post a Comment