Simple Logic to Generate Human Verification CAPTCHA Code in C#

Introduction

After researching image processing I found this simple logic to generate a CAPTCHA code for human confirmation. This simple image generation using graphics may help the developer to generate a CAPTCHA code in a better way.

Concept behind the logic

Using the advantage of graphics, import a custom image into graphics as a draw sheet and render the text and whatever we require to be drawn in the CAPTCHA image. The complexity of the graphics used to generate the image is directly proportional to the complexity of the CAPTCHA image.

The following is the C# code behind the approach.

public partial class Form1 : Form

{

        public Form1()

        {

            InitializeComponent();

            generateImage();

        }

        int value = 0;

        private void generateImage()

        {

            Random random = new Random (); // get a random instance

            value = random.Next(10000, 99999); // get a random value between any range

            var image = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height); // Get a bitmap

            var font = new Font("TimesNewRoman", 25, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel); // Get a font

            var graphics = Graphics.FromImage(image); // Get a graphics with the bitmap image

            graphics.DrawString(value.ToString (), font, Brushes.Red, new PointF(0, 0)); // Add the value in the graphics

            Pen p = new Pen(Brushes.Orange, 2.0f); // get pen width

            graphics.DrawLine(p, new PointF(0,this.pictureBox1 .Height), new Point(this.pictureBox1.Width,0)); // draw a diagonal line

            graphics.DrawLine(p, new PointF(0,0), new Point(this.pictureBox1.Width , this.pictureBox1.Height)); // draw another diagonal line

            p.Dispose(); // dispose the pen to avoid memory leak

            graphics.SmoothingMode = SmoothingMode.AntiAlias; // Smoothing the pixel

            graphics.TextRenderingHint = TextRenderingHint.AntiAlias; // Smoothing the text rendering because stem width may differ

            this.pictureBox1.Image = image; // load the image in the picturebox

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            this.generateImage();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            if (this.textBox1.Text == value.ToString())

            {

                MessageBox.Show("Long live Tamil! வாழ்க தமிழ்!");

            }

            else

            {

                MessageBox.Show("the value you have entered is wrong");

                textBox1.Text = String.Empty;

            }

        }

} 

partial class Form1

{

        /// <summary>

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.IContainer components = null;

        /// <summary>

        /// Clean up any resources being used.

        /// </summary>

        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

 

        #region Windows Form Designer generated code

 

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.button1 = new System.Windows.Forms.Button();

            this.pictureBox1 = new System.Windows.Forms.PictureBox();

            this.textBox1 = new System.Windows.Forms.TextBox();

            this.label1 = new System.Windows.Forms.Label();

            this.button2 = new System.Windows.Forms.Button();

            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();

            this.SuspendLayout();

            //

            // button1

            //

            this.button1.Location = new System.Drawing.Point(112, 122);

            this.button1.Name = "button1";

            this.button1.Size = new System.Drawing.Size(93, 23);

            this.button1.TabIndex = 0;

            this.button1.Text = "Enter";

            this.button1.UseVisualStyleBackColor = true;

            this.button1.Click += new System.EventHandler(this.button1_Click);

            //

            // pictureBox1

            //

            this.pictureBox1.Location = new System.Drawing.Point(112, 41);

            this.pictureBox1.Name = "pictureBox1";

            this.pictureBox1.Size = new System.Drawing.Size(93, 25);

            this.pictureBox1.TabIndex = 1;

            this.pictureBox1.TabStop = false;

            //

            // textBox1

            //

            this.textBox1.Location = new System.Drawing.Point(112, 72);

            this.textBox1.Name = "textBox1";

            this.textBox1.Size = new System.Drawing.Size(93, 20);

            this.textBox1.TabIndex = 2;

            //

            // label1

            //

            this.label1.AutoSize = true;

            this.label1.Location = new System.Drawing.Point(0, 75);

            this.label1.Name = "label1";

            this.label1.Size = new System.Drawing.Size(106, 13);

            this.label1.TabIndex = 3;

            this.label1.Text = "Enter the above text ";

            //

            // button2

            //

            this.button2.Location = new System.Drawing.Point(221, 41);

            this.button2.Name = "button2";

            this.button2.Size = new System.Drawing.Size(77, 28);

            this.button2.TabIndex = 4;

            this.button2.Text = "Refresh";

            this.button2.UseVisualStyleBackColor = true;

            this.button2.Click += new System.EventHandler(this.button2_Click);

            //

            // Form1

            //

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(309, 181);

            this.Controls.Add(this.button2);

            this.Controls.Add(this.label1);

            this.Controls.Add(this.textBox1);

            this.Controls.Add(this.pictureBox1);

            this.Controls.Add(this.button1);

            this.Name = "Form1";

            this.Text = "Tamil";

            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();

            this.ResumeLayout(false);

            this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Button button1;

    private System.Windows.Forms.PictureBox pictureBox1;

    private System.Windows.Forms.TextBox textBox1;

    private System.Windows.Forms.Label label1;

    private System.Windows.Forms.Button button2;

} 

Screen Shot

Captcha 

Up Next
    Ebook Download
    View all
    Learn
    View all