Create your own Captcha

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

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)

1
{
{

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)

1
2
{
<span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;">var</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;"> mch = </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Enumerable</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.Range(35, 2).Select(c => (</span></span><span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;">char</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">)c).Union(</span></span>
{
<span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;">var</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;"> mch = </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Enumerable</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.Range(35, 2).Select(c => (</span></span><span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;">char</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">)c).Union(</span></span>

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;

}

1
<span style="color:#008000;font-family:Courier New;font-size:small;"><span style="color:#008000;font-family:Courier New;font-size:small;"><span style="color:#008000;font-family:Courier New;font-size:small;">//Get a random brush</span></span></span>
<span style="color:#008000;font-family:Courier New;font-size:small;"><span style="color:#008000;font-family:Courier New;font-size:small;"><span style="color:#008000;font-family:Courier New;font-size:small;">//Get a random brush</span></span></span>

private Brush GetRandomBrushes()

1
2
{
<span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;">var</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;"> mbrsharr = </span></span><span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;">new</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;"> </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brush</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">[] { </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.Blue,</span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.BlueViolet, </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.Green, </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.Red,</span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.Purple, </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.DarkGreen, </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.Brown, </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.Black };</span></span>
{
<span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;">var</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;"> mbrsharr = </span></span><span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;"><span style="color:#0000ff;font-family:Courier New;font-size:small;">new</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;"> </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brush</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">[] { </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.Blue,</span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.BlueViolet, </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.Green, </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.Red,</span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.Purple, </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.DarkGreen, </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.Brown, </span></span><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;"><span style="color:#2b91af;font-family:Courier New;font-size:small;">Brushes</span></span></span><span style="font-family:Courier New;font-size:small;"><span style="font-family:Courier New;font-size:small;">.Black };</span></span>

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

1
2
}
}
}
}

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


Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.