A C# Project Of Captcha Reader Using Microsoft Office Document Imaging

Introduction: What is Captcha?

CAPTCHA is a type of challenge-response test used in computing as an attempt to ensure that the response is generated by a human being. The process usually involves a computer asking a user to complete a simple test which the computer is able to grade. 

A common type of CAPTCHA requires the user to type letters and/or digits from a distorted image that appears on the screen. Such tests are commonly used to prevent unwanted internet bots from accessing websites, since a normal human can easily read a CAPTCHA, while the bot cannot process the image letters and therefore, cannot answer properly, or at all.

images (1)

Functionality of  Microsoft Office Document Imaging (MODI)

Microsoft Office Document Imaging 2003 (MODI) adds programmability features to the document scanning and viewing tools that Microsoft Office 2002 (XP) included for the first time. Programmers can take advantage of a simple object model built around the Document and Image (page) objects to display and read a scanned document as easily as a paper document, perform optical character recognition (OCR), search for text within scanned documents, and copy and export scanned text and images. MODI provides an object model based on ‘document’ and ‘image’ (page) objects. One feature that has elicited particular interest on the Web is MODI’s ability to convert scanned images to text under program control, using its built-in OCR engine.

Captcha Reader Class:

using System.Drawing.Imaging;

using System.Security.Cryptography;

namespace Captcha

{

public partial class Form4 : Form

{

OpenFileDialog open = new OpenFileDialog();

public Form4()

{

InitializeComponent();

}

 Open dialog box and select image and also save the address of image in label

private void button1_Click(object sender, EventArgs e)

{

try

{

open.Filter = “Image Files(*.jpg; )|*.jpg;”;

if (open.ShowDialog() == DialogResult.OK)

{

pictureBox1.Image = new Bitmap(open.FileName);

label1.Text = open.FileName;

}

}

catch (Exception)

{

MessageBox.Show(“Please try again “);

}

}

 Call Change Color Method, Setname and also call Read letter Method

private void button2_Click(object sender, EventArgs e)

{

Bitmap img=null;

try

{

textBox1.Clear();

Image fre = pictureBox1.Image;

try

{

changeColorOfText(ref img,ref fre);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

string stri = setNameOfImage();

img.Save(stri);

ReadLetters(stri);

}

catch (Exception)

{

MessageBox.Show(“Please Try Again and Firstly Select an Image “);

}

}

Read letters of image by using OCR

public void ReadLetters(string directoryPath)

{

string fileExtension = Path.GetExtension(Convert.ToString(directoryPath));

string fileName = Convert.ToString(directoryPath).Replace(fileExtension,          string.Empty);

 

Check for JPG File Format

if (fileExtension == “.jpg” || fileExtension == “.JPG”)

{

try

{

//OCR Operations …

MODI.Document md = new MODI.Document();

md.Create(Convert.ToString(directoryPath));

md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);

MODI.Image image = (MODI.Image)md.Images[0];

textBox1.AppendText(image.Layout.Text);

}

catch (Exception)

{

MessageBox.Show(“This Image hasn’t a text or has a problem”,

“OCR Notifications”,

MessageBoxButtons.OK, MessageBoxIcon.Information);

}

 

File.Delete(directoryPath);//Delete temp images

}

}

Change all letters in black

public void changeColorOfText(ref Bitmap img,ref Image fre)

{

img = new Bitmap(fre);

Color c;

for (int i = 0; i < img.Width; i++)

{

for (int j = 0; j < img.Height; j++)

{

c = img.GetPixel(i, j);

int r = 0;

r = Convert.ToInt16(c.R);

int g = 0;

g = Convert.ToInt16(c.G);

int b = 0;

b = Convert.ToInt16(c.B);

int ans = (r + g + b) / 3;

if (ans > 236)

{

r = 255;

g = 255;

b = 255;

}

else

{

r = 0;

g = 0;

b = 0;

}

c = Color.FromArgb(r, g, b);

img.SetPixel(i, j, c);

}

}

}

Set the name of temporarily images

public String setNameOfImage()

{

Random ran = new Random();

char[] ch = “qwertyuiopasdfghjklmnbvcxz1234567890”.ToCharArray();

String st = “”;

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

{

st += ch[ran.Next(0, 36)];

}

byte[] bb = new byte[st.Length];

int y = 0;

foreach (char c in st.ToCharArray())

{

bb[y] = (byte)c;

y++;

}

MD5CryptoServiceProvider md = new MD5CryptoServiceProvider();

string myString = BitConverter.ToString(md.ComputeHash(bb)).Replace(“-“, ” “);

string name = myString + “.jpg”;

string path = “C:\\Users\\AhmadJ\\Documents\\Visual Studio 2010\\Projects\\temporarily images” + “\\” + name;

return path;

}

Go back to form 1

private void button3_Click(object sender, EventArgs e)

{

Cursor.Current = Cursors.WaitCursor;

Form1 f = new Form1();

Cursor.Current = Cursors.Default;

f.ShowDialog();

Application.Exit();

}

}

}

Working View

Capture

License

This article, along with any associated source code and files isn’t licensed. Feel free to use it.

 

 

Comments are closed.