ASP. net mvc module and component (2)-custom image verification code,

Source: Internet
Author: User

ASP. net mvc module and component (2)-custom image verification code,

 

Simply and directly, Let's go straight to the theme!


The following is the source code of a Verification Code class that generates random combinations of numbers and letters:

Using System; using System. drawing; using System. drawing. imaging; using System. web. UI; using System. drawing. drawing2D; using System. IO; namespace Forcheng. code {// <summary> /// the class that generates the verification Code /// </summary> public class ValidateCode {/// <summary> /// generate the verification Code /// </summary> /// <param name = "length"> specify the length of the Verification Code </param> /// <returns> </returns> public string CreateValidateCode (int length) {const string maid = "23456789 abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"; string validateStr = ""; int n = validateTemplate. length-1; // set the random value generator int seekSeek = unchecked (int) DateTime. now. ticks); Random seekRand = new Random (seekSeek); int beginSeek = 0; // generate a Random verification code for (int I = 0; I <length; I ++) {beginSeek = seekRand. next (0, n); validateStr + = validateTemplate. substring (beginSeek, 1);} return validateStr ;} /// <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 * 13.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 ();}}}}View Code

The following is the background instance code using the verification code class (generate a 5-Digit Image verification code ):

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; using System. web. security; using Forcheng. code; namespace Test. controllers {public class HomeController: controller {// <summary> /// obtain the image Verification Code /// </summary> /// <returns> </returns> public ActionResult GetValidateCode (string codeClass) {ValidateCode vCode = new ValidateCode (); string code = vCode. createValidateCode (5); byte [] bytes = vCode. createValidateGraphic (code); return File (bytes, @ "image/jpeg ");}}}View Code

The following is the instance code used on the front-end page:

<! DOCTYPE html> Effect (another password retrieval page, not the front-end code above ):

 

 

This is where we will continue.

<My blog homepage>: http://www.cnblogs.com/forcheng/

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.