Create your Own/Custom Image Captcha in simple steps.

Ndebata
Posted by in C# category on for Intermediate level | Points: 250 | Views : 10571 red flag
Rating: 4 out of 5  
 2 vote(s)

This artcle describes how to create your own image captcha.

Introduction

Captchas are used in attempts to prevent automated software from performing actions ensures that its human generated.In this arctle we are creating a Custom image, with random texts printed at random location in random colors and to add difficulty for the character recognition softwates we are drawing lines in the background. The following describes how to do it. Please note that you can make it more complex by editing the image.

Class Diagram



Class Description

WebCaptcha class contains private field rndm which is used to generate random numbers. Other properties as their name suggest are self explanatory. CaptchaImg holds the image (System.Drawing.Image) which we will create and our objective.

GetRandomCArr is a function which returns a random character array of length passed to this function as a parameter.

GetRandomBrushes is used to get a Random brush from a list of brushes.

IsValid will compare the captcha value with that of user input.

CreateImage creates our captcha image and assign the value to CaptchaImg. First we are creating a bitmap. Then we are drawing line in the background which will look like

Then we drawing characters at random location which will look like

   

 

Class Description

Please find the class description bellow.


public class WebCaptcha

{

public string Value { getset; }

public Image CaptchaImg { getset; }

public bool CaseSensitive { getset; }

private Random rndm;


public bool IsValid(string input)

{

return CaseSensitive?this.Value.Equals(input):this.Value.Equals(input, System.StringComparison.OrdinalIgnoreCase);

}


public WebCaptcha()

{

this.rndm = new Random();

char[] rmcarr = this.GetRandomCArr(6);

this.Value = new string(rmcarr);

this.CreateImage(rmcarr);

}


private void CreateImage(char[] rmcarr)

{

int FontSize = 20;

Bitmap bmp = new Bitmap(140, 55);

Graphics gImage = Graphics.FromImage(bmp);

gImage.FillRectangle(Brushes.WhiteSmoke, 0, 0, bmp.Width, bmp.Height);

//Draw lines

for (int i = 0; i < 200; i = i + 5)

{

gImage.DrawLine(Pens.LightGray, new Point(0, i + 5), new Point(i + 5, 0));

gImage.DrawLine(Pens.LightGray, new Point(0, 60 - (i + 5)), new Point(i + 5, 60));

}

//Draw random text

for (int i = 0; i < rmcarr.Length; i++)

{

gImage.DrawString(rmcarr[i].ToString(), new Font(FontFamily.GenericSansSerif, FontSize,FontStyle.Bold), GetRandomBrushes(), new PointF(5 + i * FontSize, 5 + (rndm.Next() % 2 == 0 ? 0 : 15)));

}

this.CaptchaImg = bmp;

}


//Get random char array

private char[] GetRandomCArr(int length)

{

var mch = Enumerable.Range(35, 2).Select(c => (char)c).Union(

Enumerable.Range(49, 9).Select(c => (char)c)).Union(

Enumerable.Range(64, 27).Select(c => (char)c)).Union(

Enumerable.Range(97, 26).Select(c => (char)c)).ToArray();

var mcstr = Enumerable.Range(1, length).Select(a => mch[rndm.Next() % mch.Length]).ToArray();

return mcstr;

}


//Get a random brush

private Brush GetRandomBrushes()

{

var mbrsharr = new Brush[] { Brushes.Blue,Brushes.BlueViolet, Brushes.Green, Brushes.Red,Brushes.Purple, Brushes.DarkGreen, Brushes.Brown, Brushes.Black };

return mbrsharr[rndm.Next() % mbrsharr.Length];

}

}



Conclusion

Now Image is generated, you can use it wherever you want.

Please note that, you can make it more complex always if you want. 

Thanks,

Debata

Page copy protected against web site content infringement by Copyscape

About the Author

Ndebata
Full Name: Narayan Debata
Member Level: Starter
Member Status: Member
Member Since: 4/12/2011 6:43:18 AM
Country: India

http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Posted by: Prabhakar on: 4/19/2011 | Points: 25
Nice Article . . . .
Posted by: Ndebata on: 4/20/2011 | Points: 25
@Prabhakar Thank you for your appreciation .
Posted by: Susanthampy on: 6/14/2011 | Points: 25
nice article
Posted by: Chvrsri on: 7/8/2011 | Points: 25
HI Dude,

Nice Share !!!!

Login to post response

Comment using Facebook(Author doesn't get notification)