Front desk
<span style= "Cursor:pointer; Text-decoration:underline "> Change a </span>
Controller
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;using Utility; Using Jellal**;namespace sjlwebsite. controllers{public class Commoncontroller:controller { #region code [OutputCache (Duration = 0)] Public ActionResult Vcode () { String code = Validatecode.createrandomcode (4); session["Vcode"] = code; Validatecode.createimage (code); return View (); } public string GetCode () { return session["Vcode"]. Tostr (); } #endregion }}
Verification Code Class
Using system;using system.collections;using system.componentmodel;using system.data;using System.Drawing;using System.web;using system.web.sessionstate;using system.web.ui;using system.web.ui.webcontrols;using System.web.ui.htmlcontrols;namespace utility{//<summary>//Perfect Random Verification code 0.10//verion:0.10/ Description: Randomly generated set verification code, and randomly rotated a certain angle, font color different///</summary> public class Validatecode {//< summary>///Generate random Code//</summary>//<param name= "Length" > Random code number www.52mvc.co m</param>//<returns></returns> public static string createrandomcode (int length) {int Rand; char code; string randomcode = String.Empty; Generate a certain length of verification code system.random random = new Random (); for (int i = 0; i < length; i++) {rand = random. Next (); If (rand% 3 = = 0) {code = (char) (' A ' + (char) (rand% 26)); } else {code = (char) (' 0 ' + (char) (rand% 10)); } Randomcode + = code. ToString (); } return Randomcode; }///<summary>//Create random code images///</summary>//<param name= "Randomco De "> Random code </param> public static void CreateImage (string randomcode) {int randangle = 45 ; Random rotation angle int mapwidth = (int) (Randomcode. Length * 23); Bitmap map = new Bitmap (Mapwidth, 28);//Create picture background graphics graph = graphics.fromimage (map); Graph. Clear (Color.aliceblue);//clears the screen and fills the background graph. DrawRectangle (New Pen (Color.Black, 0), 0, 0, map. Width-1, map. HEIGHT-1);//Draw a border//graph. SmoothingMode = system.drawing.drawing2d.smoothingmode.antialias;//mode Random rand = New Random (); Background noise generation Www.jb51.net pen Blackpen = new Pen (color.lightgray, 0); for (int i = 0; i < i++) {int x = rand. Next (0, map. Width); int y = rand. Next (0, map. Height); Graph. DrawRectangle (Blackpen, x, Y, 1, 1); }//Verification code rotation to prevent machine recognition char[] chars = Randomcode. ToCharArray ();//Break strings into single-character arrays//StringFormat format = new StringFormat in text spacing (STRINGFORMATFLAGS.NOCL IP); Format. Alignment = Stringalignment.center; Format. LineAlignment = Stringalignment.center; Definition color color[] c = {color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.da Rkcyan, color.purple}; Define font string[] Font = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "Song Body"}; for (int i = 0; i < chars. Length; i++) {int Cindex = rand. Next (7); int findex = rand. Next (5); Font f = new System.Drawing.Font (Font[findex], System.Drawing.FontStyle.Bold);//font style (parameter 2 is font size) Brush b = new System.Drawing.SolidBrush (C[cindex]); Point dot = new Point (16, 16); Graph. DrawString (dot. X.tostring (), Fontstyle,new SolidBrush (Color.Black), 10,150);//test x-coordinate display spacing of float angle = rand. Next (-randangle, randangle);//rotation of the degree graph. TranslateTransform (dot. X, Dot. Y);//move the cursor to the specified position graph. RotateTransform (angle); Graph. DrawString (chars. ToString (), F, B, 1, 1, format); Graph. DrawString (chars. ToString (), Fontstyle,new SolidBrush (Color.Blue), 1,1,format); Graph. RotateTransform (-angle);//Turn back to graph. TranslateTransform (2,-dot. Y);//move cursor to specified position}//graph. DrawString (randomcode,fontstyle,new SolidBrush (Color.Blue), 2,2); Standard random code//Birthinto picture System.IO.MemoryStream ms = new System.IO.MemoryStream (); Map. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF); HttpContext.Current.Response.ClearContent (); HttpContext.Current.Response.ContentType = "Image/gif"; HttpContext.Current.Response.BinaryWrite (Ms. ToArray ()); Graph. Dispose (); Map. Dispose (); } }}