In this article I will explain with an example, how to implement
Captcha in Windows Forms (WinForms) Application using C# and VB.Net.
Download ASPSnippets Captcha DLL
You can download the latest ASPSnippets.Captcha.dll from the following link.
Note: You will need to add the reference of ASPSnippets.Captcha DLL in your project.
Form Design
The Form consists of following controls:
Label – For labelling TextBoxes.
TextBox – For capturing PersonId, Name, City.
Captcha implementation
Following controls are required for
Captcha implementation.
PictureBox – For displaying
Captcha.
TextBox – For capturing
Captcha answer.
ErrorProvider – For displaying error icon.
Here, ErrorProvider control is used for displaying error for different cases.
Cases
· When TextBox is empty.
· When Captcha answer is invalid.
Adding reference of ASPSnippets.Captcha DLL in project
1. Inside the Solution Explorer, right click on Reference option of your Project and then click on Add Reference.
2. Then, inside the Reference Manager window, click on Browse button.
3. Locate the ASPSnippets.Captcha and select it.
4. The ASPSnippets.Captcha DLL is now visible in the Reference Manager window. Select it and click OK button as shown below.
Finally, the ASPSnippets.Captcha DLL is now referenced.
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Drawing;
using ASPSnippets.Captcha;
VB.Net
Imports System.IO
Imports System.Drawing
Imports ASPSnippets.Captcha
Implementing Arithmetic Captcha using C# and VB.Net
In the code-behind, a property of class
Captcha is created.
Form_Load
Inside the Form_Load event handler,
Captcha object is initialized and its width, height, Font size, Font color, background color and Mode are set.
Note: The ASPSnippets.Captcha works in four different Modes i.e. Numeric, Alphabets, AlphaNumeric and Arithmetic. This article will use AlphaNumeric mode.
Then, using
ImageData property of
Captcha class the BASE64 string is fetched and splatted with comma (,) and converted to BYTE array using
FromBase64String method of
Convert class.
Finally, the BYTE array is converted to Image using MemoryStream class which is then displayed using the PictureBox control.
C#
private Captcha Captcha { get; set; }
private void Form1_Load(object sender, EventArgs e)
{
this.Captcha = new Captcha(130, 40, 20f, "#FFFFFF", "#61028D", Mode.AlphaNumeric);
string base64String = this.Captcha.ImageData.Split(',')[1];
//Convert Base64 Encoded string to Byte Array.
byte[] imageBytes = Convert.FromBase64String(base64String);
//Convert Byte Array to Image and display in PictureBox.
imgCaptcha.Image = Image.FromStream(new MemoryStream(imageBytes));
}
VB.Net
Private Property Captcha As Captcha
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Captcha = New Captcha(130, 40, 20.0F, "#FFFFFF", "#61028D", Mode.AlphaNumeric)
Dim base64String As String = Me.Captcha.ImageData.Split(","c)(1)
'Convert Base64 Encoded string to Byte Array.
Dim imageBytes As Byte() = Convert.FromBase64String(base64String)
'Convert Byte Array to Image and display in PictureBox.
imgCaptcha.Image = Image.FromStream(New MemoryStream(imageBytes))
End Sub
Validating Captcha using C# and VB.Net
When
Submit button is clicked, the
ErrorProvider is set to empty and a check is performed whether the
Captcha answer TextBox is empty or not.
If found empty or NULL, then
Captcha required message is displayed using
ErrorProvider control.
If found text in the TextBox, then
Captcha is validated using
IsValid method of
Captcha class.
If IsValid method return FALSE, then Error Message is displayed using ErrorProvider.
C#
private void OnSubmit(object sender, EventArgs e)
{
epError.SetError(txtCaptchaAnswer, string.Empty);
if (string.IsNullOrEmpty(txtCaptchaAnswer.Text.Trim()))
{
epError.SetError(txtCaptchaAnswer, "Captcha required");
}
else
{
if (!this.Captcha.IsValid(txtCaptchaAnswer.Text.Trim()))
{
epError.SetError(txtCaptchaAnswer, "Captcha invalid");
}
else
{
//Perform operations here.
int personId = int.Parse(txtPersonId.Text.Trim());
string name = txtName.Text.Trim();
string gender = cbGender.Text;
string city = txtCity.Text.Trim();
}
}
}
VB.Net
Private Sub OnSubmit(sender As Object, e As EventArgs) Handles btnSubmit.Click
epError.SetError(txtCaptchaAnswer, String.Empty)
If String.IsNullOrEmpty(txtCaptchaAnswer.Text.Trim()) Then
epError.SetError(txtCaptchaAnswer, "Captcha required")
Else
If Not Me.Captcha.IsValid(txtCaptchaAnswer.Text.Trim()) Then
epError.SetError(txtCaptchaAnswer, "Captcha invalid")
Else
'Perform operations here.
Dim personId As Integer = Integer.Parse(txtPersonId.Text.Trim())
Dim name As String = txtName.Text.Trim()
Dim gender As String = cbGender.Text
Dim city As String = txtCity.Text.Trim()
End If
End If
End Sub
Screenshot
Downloads