What Captcha stand for?
Completely Automated Public Turing test to tell Computers and Humans Apart. The Captcha technology help you to make sure your site is reasonably secure against automated attacks.
Write the following code in a class named Captcha:
C#
1
public class Captcha
2
{
3
//make the captcha image for text
4
public Bitmap MakeCaptchaImage(string txt, int width, int hight, string fontFamilyName)
5
{
6
//make the bitmap and the associated Graphics object
7
Bitmap bm = new Bitmap(width, hight);
8
Graphics gr = Graphics.FromImage(bm);
9
gr.SmoothingMode = SmoothingMode.HighQuality;
10
RectangleF recF = new RectangleF(0, 0, width, hight);
11
Brush br;
12
br = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White);
13
gr.FillRectangle(br, recF);
14
SizeF text_size;
15
Font the_font;
16
float font_size = hight + 1;
17
do
18
{
19
font_size -= 1;
20
the_font = new Font(fontFamilyName, font_size, FontStyle.Bold, GraphicsUnit.Pixel);
21
text_size = gr.MeasureString(txt, the_font);
22
}
23
while ((text_size.Width > width) || (text_size.Height > hight));
24
// Center the text.
25
StringFormat string_format = new StringFormat();
26
string_format.Alignment = StringAlignment.Center;
27
string_format.LineAlignment = StringAlignment.Center;
28
29
// Convert the text into a path.
30
GraphicsPath graphics_path = new GraphicsPath();
31
graphics_path.AddString(txt, the_font.FontFamily, 1, the_font.Size, recF, string_format);
32
33
//Make random warping parameters.
34
Random rnd = new Random();
35
PointF[] pts = { new PointF((float)rnd.Next(width) / 4, (float)rnd.Next(hight) / 4), new PointF(width - (float)rnd.Next(width) / 4, (float)rnd.Next(hight) / 4), new PointF((float)rnd.Next(width) / 4, hight - (float)rnd.Next(hight) / 4), new PointF(width - (float)rnd.Next(width) / 4, hight - (float)rnd.Next(hight) / 4) };
36
Matrix mat = new Matrix();
37
graphics_path.Warp(pts, recF, mat, WarpMode.Perspective, 0);
38
39
// Draw the text.
40
br = new HatchBrush(HatchStyle.LargeConfetti, Color.LightGray, Color.DarkGray);
41
gr.FillPath(br, graphics_path);
42
43
// Mess things up a bit.
44
int max_dimension = System.Math.Max(width, hight);
45
for (int i = 0; i <= (int)width * hight / 30; i++)
46
{
47
int X = rnd.Next(width);
48
int Y = rnd.Next(hight);
49
int W = (int)rnd.Next(max_dimension) / 50;
50
int H = (int)rnd.Next(max_dimension) / 50;
51
gr.FillEllipse(br, X, Y, W, H);
52
}
53
for (int i = 1; i <= 5; i++)
54
{
55
int x1 = rnd.Next(width);
56
int y1 = rnd.Next(hight);
57
int x2 = rnd.Next(width);
58
int y2 = rnd.Next(hight);
59
gr.DrawLine(Pens.DarkGray, x1, y1, x2, y2);
60
}
61
for (int i = 1; i <= 5; i++)
62
{
63
int x1 = rnd.Next(width);
64
int y1 = rnd.Next(hight);
65
int x2 = rnd.Next(width);
66
int y2 = rnd.Next(hight);
67
gr.DrawLine(Pens.LightGray, x1, y1, x2, y2);
68
}
69
graphics_path.Dispose();
70
br.Dispose();
71
the_font.Dispose();
72
gr.Dispose();
73
return bm;
74
}
75
}
Usage:
C#
1
Captcha oCaptcha = new Captcha();
2
Random rnd = new Random();
3
string[] s = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
4
int i;
5
StringBuilder sb = new StringBuilder(4);
6
for (i = 0; i <= 4; i++)
7
{
8
sb.Append(s[rnd.Next(1, s.Length)]);
9
}
10
Bitmap bm = oCaptcha.MakeCaptchaImage(sb.ToString(), 200, 100, "Arial");
11
string url = Server.MapPath("~/Images/Captcha.bmp");
12
bm.Save(url);
13
Image1.ImageUrl = "~/
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.