CAPTCHA in WPF Using C#

Introduction

This article explains how to create a CAPTCHA in WPF using the C# and XAML . CAPTCHA is an image of random words, like afs43, ahd4kj and so on . CAPTCHA stands for "Completely Automated Public Tuning Test" that determines whether or not the user is a human being.

Many users use automated programs to block other users from registering for a PGL account. In order to stop this blockings (by an automated program) we use a CAPTCHA. It is also used to prevent bots from registering for free email accounts and email addresses.

Step 1

First we create a WPF application using the following procedure:

  • Open the Visual Studio.
  • Select "New Project".
  • Select the WPF Application.
  • Name the project "Captcha in wpf".
  • Click the "OK" button.
c1.jpg

Step 2 

Now we use some tools from the toolbox .

  • So now go to "View" -> "Toolbox".
  • Then Drag and Drop 1 textbox and 1 button onto the design view. 
C2.jpg
  • Select the TextBox and press F4 .
  • Change the background, font style, font weight, font size and foreground.

Step 3

After the changes, the final source code of the MainWindow.xaml is as in the following:

<Window x:Class="Ceptcha_in_WPF.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="350" Width="525" Background="{x:Null}">

    <Grid Background="{x:Null}" Visibility="Visible">

        <TextBox Height="53" HorizontalAlignment="Left" FontSize="28" Foreground="Red" FontStyle="Italic" FontWeight="Heavy" Margin="86,105,0,0" Name="textBox1" VerticalAlignment="Top" Width="202" Background="{x:Null}" FontStretch="UltraCondensed"></TextBox>

        <Button Content="Click" Height="31" HorizontalAlignment="Left" Margin="112,200,0,0" Name="button1" VerticalAlignment="Top" Width="161" Click="button1_Click" />

    </Grid>

</Window>

 

Step 4

  • Go to the MainWindow.xaml.cs.
  • Write the following code in MainWindows.xaml.cs.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

 

namespace Ceptcha_in_WPF

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

 

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            String allowchar = " ";

            allowchar = "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";

            allowchar += "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,y,z";

            allowchar += "1,2,3,4,5,6,7,8,9,0";

            char [] a = {','};

            String [] ar = allowchar.Split(a);

            String pwd = " ";

            string temp = " ";

            Random r = new Random();

       

            for (int i = 0; i < 6; i++)

            {

                temp = ar [(r.Next(0,ar.Length))];

           

                pwd += temp;

            }

           

            textBox1.Text =pwd ;

        }

    }

}

Output

Then press F5.

Clipbc5.jpg

Now click on the button.

c3.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all