Friday, May 6, 2011

Captcha image generator using C#

You can use following function to generate new Captcha Image using C#.

public static string NewCaptchaImage()
{
    string RandomHexValue = Guid.NewGuid().ToString("N").Substring(0, 8);
    Bitmap B = new Bitmap(HttpContext.Current.Server.MapPath("~/Images/SystemPic/VerCodeMain.JPG"));
   
    Graphics G = Graphics.FromImage(B);
    G.DrawString(RandomHexValue, new Font("Arial", 23.0f, FontStyle.Underline, GraphicsUnit.Pixel), new SolidBrush(Color.White), B.Width / 5, B.Height / 4);
    B.Save(HttpContext.Current.Server.MapPath("~/Images/SystemPic/VerCodeMainShow.JPG"));
    B.Dispose();
    return RandomHexValue; // Return string for matching
}