At present, the machine identification verification code has been quite powerful, more common to avoid the machine recognition method, is to connect the verification code string together, so that the difficulty of recognition, after all, the machine does not have artificial intelligence. I looked for a lot of. NET Generate picture Verification code example, later after some modification and collation, almost satisfied my requirements: The picture has noise point, each character's font is random, the color is random, the character part overlaps.
So how does it work?
1. First, verify the Code picture label link to the ASPX page that generated the picture, such as:
[C-sharp]View Plaincopyprint?
- <img src="identifyingcode.aspx" src= "identifyingcode.aspx" style= "Vertical-align:bottom; margin-bottom:1px; Cursor:pointer; "Style=" vertical-align:bottom; margin-bottom:1px; Cursor:pointer; "Alt=" click Refresh "onclick=" javascript:var time = new Date (). GetTime (); This.src=this.src + '? ' + Time; " >
The role of the OnClick event is that, after clicking, it will reload the page that generated the image because of the src change, in order to achieve the effect of updating the verification code.
2. Generate the Picture page code, the ASPX page does not need to add any code, Aspx.cs code, in the Pageload method, generate the picture.
[C-sharp]View Plaincopyprint?
- 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.Drawing.Imaging;
- Using System.IO;
- Namespace ATA.OLSD.OrgMng.WebSite
- {
- Public partial class IdentifyingCode:System.Web.UI.Page
- {
- private void Page_Load (object sender, System.EventArgs e)
- {
- string randomcode = this . Createrandomcode (4);
- RESPONSE.COOKIES.ADD (new HttpCookie ("Checkcode", Randomcode));
- //viewstate["Validatecode"] = Randomcode;//Session, Cookie, ViewState can be saved, according to the actual situation
- This . CreateImage (Randomcode);
- }
- // <summary>
- // Generate random code
- // </summary>
- /// <param name= "Length" > Random code number </param>
- // <returns></returns>
- Private string createrandomcode (int length)
- {
- int Rand;
- char code;
- string randomcode = String.Empty;
- //Generate a certain length of verification code
- System.Random random = new Random ();
- For (int i = 0; i < length; i++)
- {
- Rand = random. Next ();
- if (rand% 3 = = 0)
- {
- Code = (char) (' A ' + (char) (rand% 26));
- }
- Else
- {
- Code = (char) (' 0 ' + (char) (rand% 10));
- }
- Randomcode + = code. ToString ();
- }
- return randomcode;
- }
- // <summary>
- // Create random code images
- // </summary>
- /// <param name= "Randomcode" > Random code </param>
- private void CreateImage (string randomcode)
- {
- int randangle = 45; //Random rotation angle
- int mapwidth = (int) (Randomcode. Length * 16);
- Bitmap map = new Bitmap (Mapwidth, 28); //Create a picture background, set its length and width
- Graphics graph = graphics.fromimage (map);
- Graph. Clear (Color.aliceblue);
- Graph. DrawRectangle (new Pen (color.black, 0), 0, 0, map. Width-1, map. HEIGHT-1); //Draw a border
- Random rand = new Random ();
- //Generate background noise
- Pen Blackpen = New Pen (color.lightgray, 0);
- For (int i = 0; i <; i++)
- {
- int x = rand. Next (0, map. Width);
- int y = rand. Next (0, map. Height);
- Graph. DrawRectangle (Blackpen, x, Y, 1, 1);
- }
- //Verification code rotation to prevent machine identification
- char[] chars = Randomcode. ToCharArray (); //Break strings into single-character arrays
- //Text spacing
- StringFormat format = new StringFormat (Stringformatflags.noclip);
- Format. Alignment = Stringalignment.center;
- Format. LineAlignment = Stringalignment.center;
- //define a random color list
- Color[] C = {color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.darkcyan, COLOR.PURPL e};
- //define random font font
- string[] Font = { "Verdana", "Microsoft Sans Serif", " Comic Sans MS", "Arial", "song Body"};
- For (int i = 0; i < chars. Length; i++)
- {
- int cindex = rand. Next (7);
- int findex = rand. Next (5);
- Font f = new System.Drawing.Font (Font[findex], System.Drawing.FontStyle.Bold); //font style (parameter 2 is font size)
- Brush B = new System.Drawing.SolidBrush (C[cindex]);
- Point dot = new Point (11, 11); //The larger the value in parentheses, the greater the spacing between characters
- float angle = rand. Next (0, Randangle); //Rotation degree, if 0 is changed to-randangle, then the rotation angle is-45 degrees ~45 degrees
- Graph. TranslateTransform (dot. X, Dot. Y);
- Graph. RotateTransform (angle);
- Graph. DrawString (Chars[i]. ToString (), F, B, 2, 6, format); //4th, 5 parameters control left and top spacing
- Graph. RotateTransform (-angle);
- Graph. TranslateTransform (2,-dot. Y);
- }
- //Generate pictures
- System.IO.MemoryStream ms = new System.IO.MemoryStream ();
- Map. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
- Response.clearcontent ();
- Response.ContentType = "Image/gif";
- Response.BinaryWrite (Ms. ToArray ());
- Graph. Dispose ();
- Map. Dispose ();
- }
- }
- }
You can modify the character size, rotation angle, font, color, and so on for the verification code based on the comments. Each time you reload the page, you will receive a new verification code, saved in session or cookie, and read from the session or cookie at the time of verification.
The effect is as follows:
You can click Refresh.
NET generate picture verification code--turn from lisliefor