Rank: Member Groups: Member
Joined: 9/26/2008 Posts: 3 Points: 9
|
[b]Create a page with name “Captcha.aspx” Source:
Add the following code to HTML: IMG height="30" alt="" src=BuildCaptcha.aspx width="80"> asp:TextBox runat="Server" ID="txtCaptcha">
Add a button, And In button click add the following
if (Page.IsValid && (txtCaptcha.Text.ToString() == Session["RandomStr"].ToString())) { Response.Write("Code verification Successful"); } else { Response.Write( "Please enter info correctly"); }
Create a new file called “BuildCaptcha.aspx
Source: 1: Bitmap objBMP = new Bitmap(60, 20);
2: Graphics objGraphics = Graphics.FromImage(objBMP);
3: objGraphics.Clear(Color.Wheat);
4: objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
5:
6:
7: //' Configure font to use for text
8: Font objFont = new Font("Arial", 8, FontStyle.Italic);
9: string randomStr = "";
10: char[] myArray = new char[5];
11: int x;
12:
13: //That is to create the random # and add it to our string
14: Random autoRand = new Random();
15: for (x = 0; x < 5; x++)
16: {
17: myArray[x] = System.Convert.ToChar(autoRand.Next(65,90));
18: randomStr += (myArray[x].ToString());
19: }
20:
21: //This is to add the string to session, to be compared later
22: Session.Add("RandomStr", randomStr);
23:
24: //' Write out the text
25: objGraphics.DrawString(randomStr, objFont, Brushes.Red, 3, 3);
26:
27: //' Set the content type and return the image
28: Response.ContentType = "image/GIF";
29: objBMP.Save(Response.OutputStream, ImageFormat.Gif);
30: objFont.Dispose();
31: objGraphics.Dispose();
32: objBMP.Dispose();
Now access captcha.aspx from browser
|