Create a digital verification code and a digital verification code

Source: Internet
Author: User

Create a digital verification code and a digital verification code

1 public class ValidateCode 2 {3 public ValidateCode () 4 {5} 6 /// <summary> 7 // Maximum length of the verification code 8 /// </summary> 9 public int MaxLength 10 {11 get {return 10 ;} 12} 13 // <summary> 14 // the minimum length of the verification code is 15 /// </summary> 16 public int MinLength 17 {18 get {return 1 ;} 19} 20 // <summary> 21 // generate Verification Code 22 /// </summary> 23 // <param name = "length"> specify the length of the Verification Code </param> 24 // <returns> Verification code string </returns> 25 public string CreateValidateCode (int length) 26 {27 int [] randMembers = new int [length]; 28 int [] validateNums = new int [length]; 29 string validateNumberStr = ""; 30 // generate the Starting sequence value 31 int seekSeek = unchecked (int) DateTime. now. ticks); 32 Random seekRand = new Random (seekSeek); 33 int beginSeek = (int) seekRand. next (0, Int32.MaxValue-length * 10000); 34 int [] seeks = new int [length]; 35 for (int I = 0; I <length; I ++) 36 {37 beginSeek + = 10000; 38 seeks [I] = beginSeek; 39} 40 // generate random number 41 for (int I = 0; I <length; I ++) 42 {43 Random rand = new Random (seeks [I]); 44 int pownum = 1 * (int) Math. pow (10, length); 45 randMembers [I] = rand. next (pownum, Int32.MaxValue); 46} 47 // extract random numbers 48 for (int I = 0; I <length; I ++) 49 {50 string numStr = randMembers [I]. toString (); 51 int numLength = numStr. length; 52 Random rand = new Random (); 53 int numPosition = rand. next (0, numLength-1); 54 validateNums [I] = Int32.Parse (numStr. substring (numPosition, 1); 55} 56 // generate the verification code 57 for (int I = 0; I <length; I ++) 58 {59 validateNumberStr + = validateNums [I]. toString (); 60} 61 return validateNumberStr; 62} 63 // ==================================== in webform ===========================64 /// <summary> 65 /// image of the Verification Code created 66 /// </ summary> 67 // <param name = "containsPage"> page object to be output </param> 68 // <param name = "validateNum"> Verification Code </param> 69 public void CreateValidateGraphic (string validateCode, httpContext context) 70 {71 Bitmap image = new Bitmap (int) Math. ceiling (validateCode. length * 12.0), 22); 72 Graphics g = Graphics. fromImage (image); 73 try 74 {75 // generate Random generator 76 random Random = new Random (); 77 // clear image background color 78g. clear (Color. white); 79 // The interference line of the picture is 80 for (int I = 0; I <25; I ++) 81 {82 int x1 = random. next (image. width); 83 int x2 = random. next (image. width); 84 int y1 = random. next (image. height); 85 int y2 = random. next (image. height); 86g. drawLine (new Pen (Color. silver), x1, y1, x2, y2); 87} 88 Font font = new Font ("Arial", 12, (FontStyle. bold | FontStyle. italic); 89 LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, image. width, image. height), 90 Color. blue, Color. darkRed, 1.2f, true); 91g. drawString (validateCode, font, brush, 3, 2); 92 // foreground disturbance of the picture to be painted 93 for (int I = 0; I <100; I ++) 94 {95 int x = random. next (image. width); 96 int y = random. next (image. height); 97 image. setPixel (x, y, Color. fromArgb (random. next (); 98} 99 // draw the border line of the image 100g. drawRectangle (new Pen (Color. silver), 0, 0, image. width-1, image. height-1); 101 // Save image data 102 MemoryStream stream = new MemoryStream (); 103 image. save (stream, ImageFormat. jpeg); 104 // The output image stream is 105 context. response. clear (); 106 context. response. contentType = "image/jpeg"; 107 context. response. binaryWrite (stream. toArray (); 108} 109 finally110 {111g. dispose (); 112 image. dispose (); 113} 114} 115 // <summary> 116 // The length of the image for obtaining the verification code is 117 /// </summary> 118 /// <param name = "validateNumLength"> verification code length </param> 119 // <returns> </returns> 120 public static int GetImageWidth (int validateNumLength) 121 {122 return (int) (validateNumLength * 12.0 ); 123} 124 // <summary> 125 // The height of the verification code is 126 /// </summary> 127 // <returns> </returns> 128 public static double getImageHeight () 129 {130 return 22.5; 131} 132 133 // ========================== in mvc ============ 134 // C # MVC upgraded version 135 /// <summary> 136 // picture of the Verification Code created 137 /// </summary> 138 /// <param name = "containsPage"> page object to be output </param> 139 /// <param name = "validateNum"> Verification Code </param> 140 public byte [] CreateValidateGraphic (string validateCode) 141 {142 Bitmap image = new Bitmap (int) Math. ceiling (validateCode. length * 12.0), 22); 143 Graphics g = Graphics. fromImage (image); 144 try145 {146 // generate a Random generator 147 random Random = new Random (); 148 // clear the image background color 149g. clear (Color. white); 150 // The interference line of the picture. 151 for (int I = 0; I <25; I ++) 152 {153 int x1 = random. next (image. width); 154 int x2 = random. next (image. width); 155 int y1 = random. next (image. height); 156 int y2 = random. next (image. height); 157g. drawLine (new Pen (Color. silver), x1, y1, x2, y2); 158} 159 Font font = new Font ("Arial", 12, (FontStyle. bold | FontStyle. italic); 160 LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, image. width, image. height), 161 Color. blue, Color. darkRed, 1.2f, true); 162g. drawString (validateCode, font, brush, 3, 2); 163 // foreground disturbance of the image painting: 164 for (int I = 0; I <100; I ++) 165 {166 int x = random. next (image. width); 167 int y = random. next (image. height); 168 image. setPixel (x, y, Color. fromArgb (random. next (); 169} 170 // draw the border line of the image 171g. drawRectangle (new Pen (Color. silver), 0, 0, image. width-1, image. height-1); 172 // Save image data 173 MemoryStream stream = new MemoryStream (); 174 image. save (stream, ImageFormat. jpeg); 175 // output image stream 176 return stream. toArray (); 177} 178 finally179 {180g. dispose (); 181 image. dispose (); 182} 183} 184}

The verification code and Verification Code image are generated above.

The following is how to use it:

1 string code = vCode. CreateValidateCode (4); // obtain the generated Verification code 2 byte [] bcode = vCode. CreateValidateGraphic (code); // create the verification code as the binary format of the image

 

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.