Generate an image verification code and a verification code

Source: Internet
Author: User

Generate an image verification code and a verification code

Public class ValidateCode {public ValidateCode () {}/// <summary> /// Maximum length of the Verification Code /// </summary> public int MaxLength {get {return 10 ;}} /// <summary> /// minimum length of the Verification Code /// </summary> public int MinLength {get {return 1 ;}} /// <summary> /// generate the verification code /// </summary> /// <param name = "length"> specify the verification code length </param> /// <returns> </returns> public string CreateValidateCode (int length) {int [] randMembers = new int [length]; int [] validateNums = new int [length]; string validateNumberStr = ""; // generate the Starting sequence value int seekSeek = unchecked (int) DateTime. now. ticks); Random seekRand = new Random (seekSeek); int beginSeek = (int) seekRand. next (0, Int32.MaxValue-length * 10000); int [] seeks = new int [length]; for (int I = 0; I <length; I ++) {beginSeek + = 10000; seeks [I] = beginSeek;} // generates a random number for (int I = 0; I <length; I ++) {Random rand = new Random (seeks [I]); int pownum = 1 * (int) Math. pow (10, length); randMembers [I] = rand. next (pownum, Int32.MaxValue) ;}// extract random numbers for (int I = 0; I <length; I ++) {string numStr = randMembers [I]. toString (); int numLength = numStr. length; Random rand = new Random (); int numPosition = rand. next (0, numLength-1); validateNums [I] = Int32.Parse (numStr. substring (numPosition, 1) ;}// generate the verification code for (int I = 0; I <length; I ++) {validateNumberStr + = validateNums [I]. toString ();} return validateNumberStr ;} /// <summary> /// the image for creating the verification code /// </summary> /// <param name = "containsPage"> the page object to be output </param> /// <param name = "validateNum"> Verification Code </param> public byte [] CreateValidateGraphic (string validateCode) {Bitmap image = new Bitmap (int) Math. ceiling (validateCode. length * 15.0), 25); Graphics g = Graphics. fromImage (image); try {// generate Random generator random Random = new Random (); // clear the image background color g. clear (Color. white); // The interference line of the picture. for (int I = 0; I <25; 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 Font ("Arial", 14, (FontStyle. bold | FontStyle. italic); LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, image. width, image. height), Color. blue, Color. darkRed, 1.2f, true); g. drawString (validateCode, font, brush, 3, 2); // foreground disturbance of the image to be painted for (int I = 0; I <100; 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 g of the image. drawRectangle (new Pen (Color. silver), 0, 0, image. width-1, image. height-1); // Save the image data. MemoryStream = new MemoryStream (); image. save (stream, ImageFormat. jpeg); // return stream of the output image stream. toArray ();} finally {g. dispose (); image. dispose ();}} /// <summary> /// obtain the length of the Verification Code image /// </summary> /// <param name = "validateNumLength"> Verification code length </param>/ // <returns> </returns> public static int GetImageWidth (int validateNumLength) {return (int) (validateNumLength * 13.0 );} /// <summary> /// height of the Verification Code obtained /// </summary> /// <returns> </returns> public static double GetImageHeight () {return 25.5 ;}}

 

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.