C # general types of perfect verification codes written

Source: Internet
Author: User

  1. Using System;
  2. Using System. Collections;
  3. Using System. ComponentModel;
  4. Using System. Data;
  5. Using System. Drawing;
  6. Using System. Web;
  7. Using System. Web. SessionState;
  8. Using System. Web. UI;
  9. Using System. Web. UI. WebControls;
  10. Using System. Web. UI. HtmlControls;
  11. Namespace WebApplication1. Verification Code
  12. {
  13. /// <Summary>
  14. /// Perfect Random verification code 0.10
  15. /// Verion: 0.10
  16. /// Description: The verification code is randomly generated and rotated to a certain angle. The font color is different.
  17. /// </Summary>
  18. Public class ValidateCode: System. Web. UI. Page
  19. {
  20. Private void Page_Load (object sender, System. EventArgs e)
  21. {
  22. String randomcode = this. CreateRandomCode (4 );
  23. Session ["ValidateCode"] = randomcode;
  24. // ViewState ["ValidateCode"] = randomcode;
  25. This. CreateImage (randomcode );
  26. }
  27. /// <Summary>
  28. /// Generate random code
  29. /// </Summary>
  30. /// <Param name = "length"> Number of random codes </param>
  31. /// <Returns> </returns>
  32. Private string CreateRandomCode (int length)
  33. {
  34. Int rand;
  35. Char code;
  36. String randomcode = String. Empty;
  37. // Generate a verification code of a certain length
  38. System. Random random = new Random ();
  39. For (int I = 0; I <length; I ++)
  40. {
  41. Rand = random. Next ();
  42. If (rand % 3 = 0)
  43. {
  44. Code = (char) (A + (char) (rand % 26 ));
  45. }
  46. Else
  47. {
  48. Code = (char) (0 + (char) (rand % 10 ));
  49. }
  50. Randomcode + = code. ToString ();
  51. }
  52. Return randomcode;
  53. }
  54. /// <Summary>
  55. /// Create a random Image
  56. /// </Summary>
  57. /// <Param name = "randomcode"> Random Code </param>
  58. Private void CreateImage (string randomcode)
  59. {
  60. Int randAngle = 45; // random Rotation Angle
  61. Int mapwidth = (int) (randomcode. Length * 23 );
  62. Bitmap map = new Bitmap (mapwidth, 28); // creates an image background.
  63. Graphics graph = Graphics. FromImage (map );
  64. Graph. Clear (Color. AliceBlue); // Clear the screen and fill in the background.
  65. Graph. DrawRectangle (new Pen (Color. Black, 0), 0, 0, map. Width-1, map. Height-1); // draw a border
  66. // Graph. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. AntiAlias; // Mode
  67. Random rand = new Random ();
  68. // Generate background noise
  69. Pen blackPen = new Pen (Color. LightGray, 0 );
  70. For (int I = 0; I <50; I ++)
  71. {
  72. Int x = rand. Next (0, map. Width );
  73. Int y = rand. Next (0, map. Height );
  74. Graph. DrawRectangle (blackPen, x, y, 1, 1 );
  75. }
  76. // Rotate the verification code to prevent Machine recognition
  77. Char [] chars = randomcode. ToCharArray (); // split the string into a single character array
  78. // Text distance
  79. StringFormat format = new StringFormat (StringFormatFlags. NoClip );
  80. Format. Alignment = StringAlignment. Center;
  81. Format. LineAlignment = StringAlignment. Center;
  82. // Define the color
  83. Color [] c = {Color. Black, Color. Red, Color. DarkBlue, Color. Green, Color. Orange, Color. Brown, Color. DarkCyan, Color. Purple };
  84. // Define the font
  85. String [] font = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", ""};
  86. For (int I = 0; I <chars. Length; I ++)
  87. {
  88. Int cindex = rand. Next (7 );
  89. Int findex = rand. Next (5 );
  90. Font f = new System. Drawing. Font (font [findex], 13, System. Drawing. FontStyle. Bold); // Font style (parameter 2 indicates the Font size)
  91. Brush B = new System. Drawing. SolidBrush (c [cindex]);
  92. Point dot = new Point (16, 16 );
  93. // Graph. DrawString (dot. X. ToString (), fontstyle, new SolidBrush (Color. Black), 10,150); // test the display spacing of X coordinates.
  94. Float angle = rand. Next (-randAngle, randAngle); // degrees of Rotation
  95. Graph. TranslateTransform (dot. X, dot. Y); // move the cursor to the specified position
  96. Graph. RotateTransform (angle );
  97. Graph. DrawString (chars [I]. ToString (), f, B, 1, 1, format );
  98. // Graph. DrawString (chars [I]. ToString (), fontstyle, new SolidBrush (Color. Blue), 1, 1, format );
  99. Graph. RotateTransform (-angle); // go back
  100. Graph. TranslateTransform (2,-dot. Y); // move the cursor to the specified position
  101. }
  102. // Graph. DrawString (randomcode, fontstyle, new SolidBrush (Color. Blue), 2, 2); // standard random code
  103. // Generate an image
  104. System. IO. MemoryStream MS = new System. IO. MemoryStream ();
  105. Map. Save (MS, System. Drawing. Imaging. ImageFormat. Gif );
  106. Response. ClearContent ();
  107. Response. ContentType = "image/gif ";
  108. Response. BinaryWrite (ms. ToArray ());
  109. Graph. Dispose ();
  110. Map. Dispose ();
  111. }
  112. # Code generated by region Web Form Designer
  113. Override protected void OnInit (EventArgs e)
  114. {
  115. //
  116. // CODEGEN: & n

Related Article

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.