Windows Forms - Creating Captcha Numbers

In this blog, we will learn how to create Captcha and how it works in the Windows forms to verify with textbox.

Captcha

Captcha is what system will use to verify the if the user is a human or a machine. Captcha has many types (ex: Number, Characters, picture, etc...)

Step 1

Click New >> Project >> Visual C# >> Windows >> Windows Forms Application. Enter your Project name and click OK.



Step 2

Click the View->Select Toolbox. We are using this Toolbox to design the form in Windows application.



Step 3

Click the Button properties and add the button1_Click Event to be completed for the event to make the function more accurate.
  1. public Form1() {  
  2.     InitializeComponent();  
  3.     loadCaptchaImage();  
  4. }  
  5. int number = 0;  
  6. private void loadCaptchaImage() {  
  7.     Random r1 = new Random();  
  8.     number = r1.Next(100, 1000);  
  9.     var image = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);  
  10.     var font = new Font("TimesNewRoman", 25, FontStyle.Bold, GraphicsUnit.Pixel);  
  11.     var graphics = Graphics.FromImage(image);  
  12.     graphics.DrawString(number.ToString(), font, Brushes.Green, new Point(0, 0));  
  13.     pictureBox1.Image = image;  
  14. }  
  15. private void button1_Click(object sender, EventArgs e) {  
  16.     loadCaptchaImage();  
  17. }  
  18. private void button2_Click(object sender, EventArgs e) {  
  19.     if (textBox1.Text == number.ToString()) {  
  20.         MessageBox.Show("Match Text with Captcha");  
  21.     } else {  
  22.         MessageBox.Show("DOes not Match Text with Captcha");  
  23.     }  
  24. }  
Step 4

Press F5 or "Build and Run" the application to get the Captcha and verify it.

Result: Captcha works.