0 Replies - 1250 Views - Last Post: 13 March 2014 - 07:40 AM

#1 mager1794  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 133
  • Joined: 23-March 09

Simple Captcha Class

Posted 13 March 2014 - 07:40 AM

Consists of two main functions
Image CreateCaptcha()
bool CompareCaptcha()

Property
CaptchaValue - returns the text version of the image created from CreateCaptcha


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace Captcha
{
    public class Captcha
    {
        Random r = new Random();
        string[] captchaletters = 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","1","2","3","4","5","6","7","8","9",
        "0"};

        string captchaval = "";

        public Image CreateCaptcha()
        {
            Font f = new Font("Arial", 16);
            Image i = new Bitmap(1000,125);
            Graphics g = Graphics.FromImage(i);

            GenerateCaptcha();
            g.DrawString(CaptchaValue,f,Brushes.Black,new PointF(0,0));


            return i;
        }

        

        private void GenerateCaptcha()
        {
            captchaval = "";

            for(int i = 1; i <= 8;i++)
            {
                captchaval += captchaletters[r.Next(captchaletters.Length - 1)];
            }
            
        }

        public string CaptchaValue
        {
            get { return captchaval; }
        }

        public bool CompareCaptcha(string input)
        {
            if (input == CaptchaValue)
                return true;




            return false;
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1