General verification code generation methods are the same, the main steps have two steps
The first step: a random number or letter of the system verification code, by the way the randomly generated numbers or letters written to the cookie or Session.
The second step: Use the first step random numbers or letters to synthesize the picture.
It can be seen that the complexity of the verification code is mainly the second step to complete, you can according to the complexity of their own to set.
Let's take a look at:
The first step: random generation of numbers or letters
<summary>/// generate a random number of verification codes///</summary>// <returns> return five-bit random number </returns> private string Generatecheckcode () { int number; char code; string checkcode = String.Empty; Random random = new random (); for (int i = 0; i < 5; i++)//You can optionally set the number of digits to generate the verification code {# = random. Next (); if (number% 2 = = 0) code = (char) (' 0 ' + (char) (number%)); else code = (char) (' A ' + (char) (number%)); Checkcode + = code. ToString (); } RESPONSE.COOKIES.ADD (New HttpCookie ("Checkcode", Checkcode));//write Cookis session["checkcode"] = Checkcode;// Write to the session, you can choose any return checkcode; }
Step two: Create a picture
<summary>///Generate CAPTCHA Pictures///</summary>//<param name= "Checkcode" ></param> private void Cre Atecheckcodeimage (String checkcode) {if (Checkcode = = NULL | | Checkcode.trim () = = String.Empty) return; Bitmap image = new Bitmap ((int) math.ceiling ((Checkcode.length * 12.5)), 22); Graphics g = graphics.fromimage (image); try {//Generate random generator randomly random = new random (); Empty the picture background color g.clear (color.white); Picture the background noise line for (int i = 0; i < i++) {int x1 = random. Next (image. Width); int x2 = random. Next (image. Width); int y1 = random. Next (image. Height); int y2 = random. Next (image. Height); G.drawline (New Pen (color.silver), x1, y1, x2, y2); } Font font = new System.Drawing.Font ("Arial", and (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, image. Width, image. Height), COLOR.BLUE, color.darkred, 1.2f, true); g.DrawString (Checkcode, Font, brush, 2, 2); Picture the foreground noise point for (int i = 0; i < i++) {int x = random. Next (image. Width); int y = random. Next (image. Height); Image. SetPixel (x, Y, Color.FromArgb (random. Next ())); }//Draw the border line of the picture G.drawrectangle (new Pen (Color.silver), 0, 0, image. Width-1, image. HEIGHT-1); MemoryStream ms = new MemoryStream (); Image. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF); Response.clearcontent (); Response.ContentType = "Image/gif"; Response.BinaryWrite (Ms. ToArray ()); } finally {//Releases Object resource G.dispose (); Image. Dispose (); }
* Full Program
Add a checkcode.aspx file to the project inside the VS2005 and add the following complete code to the CheckCode.aspx.cs code file
Using system;using system.data;using system.configuration;using system.collections;using System.Web;using System.web.security;using system.web.ui;using system.web.ui.webcontrols;using System.Web.UI.WebControls.WebParts; Using system.web.ui.htmlcontrols;using system.drawing;using system.io;using System.Drawing.Drawing2D; public partial class checkcode:system.web.ui.page{protected void Page_Load (object sender, EventArgs e) {Createche Ckcodeimage (Generatecheckcode ());//Call the following two methods; }///<summary>//Generate a random number of verification codes///</summary>//<returns> return five-bit random number </returns> private string G Eneratecheckcode () {int number; char code; string checkcode = String.Empty; Random random = new random (); for (int i = 0; i < 5; i++)//You can optionally set the number of digits to generate the verification code {# = random. Next (); if (number% 2 = = 0) code = (char) (' 0 ' + (char) (number% 10)); else Code = (char) (' A ' + (char) (number% 26)); Checkcode + = code. ToString (); } ResPonse. Cookies.add (New HttpCookie ("Checkcode", Checkcode));//write Cookis session["Checkcode"] = Checkcode; Write the session, you can choose any return checkcode; }///<summary>///Generate CAPTCHA Pictures///</summary>//<param name= "Checkcode" ></param> private Voi D createcheckcodeimage (String checkcode) {if (Checkcode = = NULL | | Checkcode.trim () = = String.Empty) return; Bitmap image = new Bitmap ((int) math.ceiling ((Checkcode.length * 12.5)), 22); Graphics g = graphics.fromimage (image); try {//Generate random generator randomly random = new random (); Empty the picture background color g.clear (color.white); Picture the background noise line for (int i = 0; i < i++) {int x1 = random. Next (image. Width); int x2 = random. Next (image. Width); int y1 = random. Next (image. Height); int y2 = random. Next (image. Height); G.drawline (New Pen (color.silver), x1, y1, x2, y2); } Font font = new System.Drawing.Font ("Arial", and (System.Drawing.FontStyle.BoLD | System.Drawing.FontStyle.Italic)); LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, image. Width, image. Height), Color.Blue, color.darkred, 1.2f, true); g.DrawString (Checkcode, Font, brush, 2, 2); Picture the foreground noise point for (int i = 0; i < i++) {int x = random. Next (image. Width); int y = random. Next (image. Height); Image. SetPixel (x, Y, Color.FromArgb (random. Next ())); }//Draw the border line of the picture G.drawrectangle (new Pen (Color.silver), 0, 0, image. Width-1, image. HEIGHT-1); MemoryStream ms = new MemoryStream (); Image. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF); Response.clearcontent (); Response.ContentType = "Image/gif"; Response.BinaryWrite (Ms. ToArray ()); } finally {//Releases Object resource G.dispose (); Image. Dispose (); } } }
The page that generated the verification code above is ready, let's call to see:
Add an image control where you need to use the verification code
<asp:image id= "Image1" runat= "Server" imageurl= "~/checkcode.aspx"/>
This will show the captcha on the image control!
The display is ready, of course, we have to determine the user's input is correct!
As long as we get the value entered by the user, the comparison with Cookis or session is OK.
The value of the cookie request.cookies["Checkcode"). Value
Take the value of Session session["Checkcode"]. ToString () (best to first determine if the session is empty)
If you do not distinguish between the case, the user input value and the value of the cookie or session will be converted to uppercase or lowercase
Attached usage
protected void Button1_Click (object sender, EventArgs e) { if (request.cookies["Checkcode"]. Value = = TextBox1.Text.Trim (). ToString ()) { Response.Write ("Cookies are Right"); } else { Response.Write ("Cookies are Wrong"); } if (session["Checkcode"]! = null) { if (session["Checkcode"]. ToString (). ToUpper () = = TextBox1.Text.Trim (). ToString (). ToUpper ()) //The write can not be case -sensitive { Response.Write ("Session is Right"); } else { Response.Write ("Session is Wrong"); } } }
The above is the whole content of this article, teach you how to make ASP, I hope you like.