MVC + EasyUI + layer-3 news website build verification code generation (3), mvceasyui

Source: Internet
Author: User

MVC + EasyUI + layer-3 news website build verification code generation (3), mvceasyui

In the NewWeb. Common class library of the project, we create a class to store the code that generates the verification code.

Here, my class name is ValidateCode.

Code of the ValidateCode class:

Using System; using System. collections. generic; using System. drawing; using System. drawing. drawing2D; using System. drawing. imaging; using System. IO; using System. linq; using System. text; using System. threading. tasks; using System. web; namespace NewWeb. common {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 = "context"> the page object to be output </param> /// <param name = "validateNum"> Verification Code </param> public void CreateValidateGraphic (string validateCode, httpContext context) {Bitmap image = new Bitmap (int) Math. ceiling (validateCode. length * 12.0), 22); 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", 12, (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); // output the image stream context. response. clear (); context. response. contentType = "image/jpeg"; context. response. binaryWrite (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 * 12.0 );} /// <summary> /// height of the Verification Code obtained /// </summary> /// <returns> </returns> public static double GetImageHeight () {return 22.5 ;} // C # upgraded MVC version /// <summary> /// image for creating the verification code /// </summary> /// <param name = "containsPage"> output page Object </param> /// <param name = "validateNum"> Verification Code </param> public byte [] CreateValidateGraphic (string validateCode) {Bitmap image = new Bitmap (int) Math. ceiling (validateCode. length * 12.0), 22); 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", 12, (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 ();}}}}

In this case, we need to introduce two namespaces: System. Drawing and System. Web.

After that, create an ActionResult in the LoginController and call ValidateCode to generate a verification code. (NewWeb. Common must be referenced first)

The verification code is as follows:

Public ActionResult ValidateCode () {Common. validateCode validateCode = new Common. validateCode (); string code = validateCode. createValidateCode (4); // The generated Verification code contains 4 length sessions ["validateCode"] = code; byte [] buffer = validateCode. createValidateGraphic (code); // create a verification code image return File (buffer, "image/jpeg"); // return an image}

Go to the index page in the Login view and add the src of img as follows:

<Tr> <td style = "width: 20px"> </td>  </td> <a href =" javascript: void (0) "onclick =" changeCheckCode (); return false; "> cannot see clearly, change one </a> </td> </tr>

The verification code is displayed on the page.

The following is how to click to switch the verification code. This is the Code implemented through js.

<Script type = "text/javascript"> $ (function () {initWin (); // initialize the login form changeCheckCode (); // switch the verification code}); function initWin () {$ ("# win "). window ({title: "login", width: 400, height: 270, collapsible: false, minimizable: false, maximizable: false, closable: false, modal: true, resizable: false,});} // switch the verification code function changeCheckCode () {$ ("# changeVcode "). click (function () {$ ("# image "). attr ("src", $ ("# image "). attr ("src") + 1) ;}</script>

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.