NET generate picture verification code--turn from lisliefor

Source: Internet
Author: User

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?
    1. <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?
  1. Using System;
  2. Using System.Data;
  3. Using System.Configuration;
  4. Using System.Collections;
  5. Using System.Web;
  6. Using System.Web.Security;
  7. Using System.Web.UI;
  8. Using System.Web.UI.WebControls;
  9. Using System.Web.UI.WebControls.WebParts;
  10. Using System.Web.UI.HtmlControls;
  11. Using System.Drawing;
  12. Using System.Drawing.Imaging;
  13. Using System.IO;
  14. Namespace ATA.OLSD.OrgMng.WebSite
  15. {
  16. Public partial class IdentifyingCode:System.Web.UI.Page
  17. {
  18. private void Page_Load (object sender, System.EventArgs e)
  19. {
  20. string randomcode = this .  Createrandomcode (4);
  21. RESPONSE.COOKIES.ADD (new HttpCookie ("Checkcode", Randomcode));
  22. //viewstate["Validatecode"] = Randomcode;//Session, Cookie, ViewState can be saved, according to the actual situation
  23. This .  CreateImage (Randomcode);
  24. }
  25. // <summary>
  26. // Generate random code
  27. // </summary>
  28. /// <param name= "Length" > Random code number </param>
  29. // <returns></returns>
  30. Private string createrandomcode (int length)
  31. {
  32. int Rand;
  33. char code;
  34. string randomcode = String.Empty;
  35. //Generate a certain length of verification code
  36. System.Random random = new Random ();
  37. For (int i = 0; i < length; i++)
  38. {
  39. Rand = random. Next ();
  40. if (rand% 3 = = 0)
  41. {
  42. Code = (char) (' A ' + (char) (rand% 26));
  43. }
  44. Else
  45. {
  46. Code = (char) (' 0 ' + (char) (rand% 10));
  47. }
  48. Randomcode + = code. ToString ();
  49. }
  50. return randomcode;
  51. }
  52. // <summary>
  53. // Create random code images
  54. // </summary>
  55. /// <param name= "Randomcode" > Random code </param>
  56. private void CreateImage (string randomcode)
  57. {
  58. int randangle = 45; //Random rotation angle
  59. int mapwidth = (int) (Randomcode.  Length * 16);
  60. Bitmap map = new Bitmap (Mapwidth, 28); //Create a picture background, set its length and width
  61. Graphics graph = graphics.fromimage (map);
  62. Graph. Clear (Color.aliceblue);
  63. Graph. DrawRectangle (new Pen (color.black, 0), 0, 0, map. Width-1, map. HEIGHT-1); //Draw a border
  64. Random rand = new Random ();
  65. //Generate background noise
  66. Pen Blackpen = New Pen (color.lightgray, 0);
  67. For (int i = 0; i <; i++)
  68. {
  69. int x = rand. Next (0, map.  Width);
  70. int y = rand. Next (0, map.  Height);
  71. Graph. DrawRectangle (Blackpen, x, Y, 1, 1);
  72. }
  73. //Verification code rotation to prevent machine identification
  74. char[] chars = Randomcode. ToCharArray (); //Break strings into single-character arrays
  75. //Text spacing
  76. StringFormat format = new StringFormat (Stringformatflags.noclip);
  77. Format. Alignment = Stringalignment.center;
  78. Format. LineAlignment = Stringalignment.center;
  79. //define a random color list
  80. Color[] C = {color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.darkcyan, COLOR.PURPL e};
  81. //define random font font
  82. string[] Font = { "Verdana", "Microsoft Sans Serif", " Comic Sans MS", "Arial", "song Body"};
  83. For (int i = 0; i < chars. Length; i++)
  84. {
  85. int cindex = rand.  Next (7);
  86. int findex = rand.  Next (5);
  87. Font f = new System.Drawing.Font (Font[findex], System.Drawing.FontStyle.Bold); //font style (parameter 2 is font size)
  88. Brush B = new System.Drawing.SolidBrush (C[cindex]);
  89. Point dot = new Point (11, 11); //The larger the value in parentheses, the greater the spacing between characters
  90. float angle = rand.  Next (0, Randangle); //Rotation degree, if 0 is changed to-randangle, then the rotation angle is-45 degrees ~45 degrees
  91. Graph. TranslateTransform (dot. X, Dot. Y);
  92. Graph. RotateTransform (angle);
  93. Graph. DrawString (Chars[i]. ToString (), F, B, 2, 6, format); //4th, 5 parameters control left and top spacing
  94. Graph. RotateTransform (-angle);
  95. Graph. TranslateTransform (2,-dot. Y);
  96. }
  97. //Generate pictures
  98. System.IO.MemoryStream ms = new System.IO.MemoryStream ();
  99. Map. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
  100. Response.clearcontent ();
  101. Response.ContentType = "Image/gif";
  102. Response.BinaryWrite (Ms. ToArray ());
  103. Graph. Dispose ();
  104. Map. Dispose ();
  105. }
  106. }
  107. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.