Asp.net mvc Verification Code class, asp. netmvc Verification Code Class
Verification Code
Namespace QJW. verifyCode {// usage: // public FileContentResult CreateValidate () // {// ValidateCode vCode = new ValidateCode (); // string code = vCode. createValidateCode (5); // Session ["ValidateCode"] = code; // byte [] bytes = vCode. createValidateGraphic (code); // return File (bytes, "image/JPEG"); //} 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 * 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 ();}} /// <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 ;}}}
Usage
public FileContentResult CreateValidate() { ValidateCode vCode = new ValidateCode(); string code = vCode.CreateValidateCode(5); Session["ValidateCode"] = code; byte[] bytes = vCode.CreateValidateGraphic(code); return File(bytes, "image/JPEG"); }
HTML
<A href = "javascript:;" onclick = "reloadcode (); "> </a>
View JS
function reloadcode() { var verify = document.getElementById('safecode'); verify.setAttribute('src', '/common/CreateValidate?' + Math.random()); }
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.