Skip to main content

Image Captcha Example in Windows Form C#



A computer program or system intended to distinguish human from machine input, typically as a way of thwarting spam and automated extraction of data from websites.
It is generally used to check whether you are a robot or a uman being.
There are two basic alternatives on how to generate the content of the captcha
  1. Submit a string that should be used
  2. Automatically generate a random string. If this alternative is chosen then the user of the library should save the created string and compare it to what the user enters.
CAPTCHA or Completely Automated Public Turing test to Tell Computers and Humans Apart is a technique to distinguish between humans and computers. CAPTCHA is mainly used as a security check to ensure only human users can pass through. Generally, computers or bots are not capable of solving a captcha.

    Add some control in web form .. take 2 button control one button control is used to generate random captcha code and second one is used to verify the code is match or not. And add one pictureBox control to show image in web form and one textBox control for verify text.

In .cs file write some code -

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LoadCaptcha();
        }


        int num = 0;
        private void LoadCaptcha()
        {
            //throw new NotImplementedException();
            Random r1 = new Random();
            num = r1.Next(100,999);
            var img = new Bitmap(this.pictureBox1.Width,this.pictureBox1.Height);
            var font = new Font("Arial",30,FontStyle.Bold,GraphicsUnit.Pixel);
            var graphics = Graphics.FromImage(img);
            graphics.DrawString(num.ToString(),font,Brushes.Red,new Point(0,0));
            pictureBox1.Image = img;

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void generateBtn_Click(object sender, EventArgs e)
        {
            LoadCaptcha();
        }

        private void verifyBtn_Click(object sender, EventArgs e)
        {
            if (textBox1.Text==num.ToString())
            {
                MessageBox.Show("captcha match");
            }
            else
            {
                MessageBox.Show("captcha not match");
            }
        }
    }
}

Comments

Popular posts from this blog

Query String Example in ASP.NET

    Query Strings in ASP.Net A QueryString is a collection of characters input to a computer or web browser. A Query String is helpful when we want to transfer a value from one page to another. When we need to pass content between the HTML pages or aspx Web Forms in the context of ASP.NET, a Query String is very easy to use and the Query String follows a separating character, usually a Question Mark (?). It is basically used for identifying data appearing after this separating symbol. A Query String Collection is used to retrieve the variable values in the HTTP query string. If we want to transfer a large amount of data then we can't use the Request.QueryString . Query Strings are also generated by form submission or can be used by a user typing a query into the address bar of the browsers. Query Strings are contained in request headers. A Query String collection is a parsed version of the QUERY_STRING variable in the Server Variables collect...

Digital Clock in ASP.NET with using C#.

Want to digital clock on web page this is way to make digital clock.