Refer to this article: http://www.cnblogs.com/yuangang/p/6000460.html
usingSystem;usingSystem.IO;usingSystem.drawingcore;usingSystem.DrawingCore.Imaging;namespacemobileweb.utility{ Public classCaptcha { Public stringCreatevalidatenumber (intlength) { int[] Randmembers =New int[length]; int[] Validatenums =New int[length]; System.Text.StringBuilder Validatenumberstr=NewSystem.Text.StringBuilder (); //generate a starting sequence value intSeekseek =unchecked((int) DateTime.Now.Ticks); Random Seekrand=NewRandom (Seekseek); intBeginseek = (int) Seekrand.next (0, Int32.maxvalue-length *10000); int[] seeks =New int[length]; for(inti =0; i < length; i++) {Beginseek+=10000; Seeks[i]=Beginseek; } //Generate random numbers for(inti =0; i < length; i++) {Random rand=NewRandom (Seeks[i]); intPownum =1* (int) Math.pow (Ten, length); Randmembers[i]=Rand. Next (Pownum, Int32.MaxValue); } //extracting random numbers for(inti =0; i < length; i++) { stringNumstr =Randmembers[i]. ToString (); intNumlength =numstr.length; Random Rand=NewRandom (); intNumposition = Rand. Next (0, Numlength-1); Validatenums[i]= Int32.Parse (numstr.substring (Numposition,1)); } //Generate verification Code for(inti =0; i < length; i++) {validatenumberstr.append (validatenums[i]. ToString ()); } returnvalidatenumberstr.tostring (); } Public byte[] Createvalidategraphic (stringvalidatenum) {Bitmap Img=NULL; Graphics g=NULL; MemoryStream Ms=NULL; Random Random=NewRandom (); //validation code Color collectionColor[] C ={color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.darkcyan, Color.Purple}; //Validation code font collection string[] fonts = {"Verdana","Microsoft Sans Serif","Comic Sans MS","Arial","Song Body" }; //defines the size of the image, creating an instance of the imageIMG =NewBitmap ((int) Validatenum.length * -, +); G= Graphics.fromimage (IMG);//generating a new Graphics object from an IMG objectg.clear (color.white);//background set to white//draw back spots at random locations for(inti =0; I < -; i++) { intx =Random. Next (Img.width); inty =Random. Next (Img.height); G.drawrectangle (NewPen (Color.lightgray,0), X, Y,1,1); } //verification code plotted in G for(inti =0; i < validatenum.length; i++) { intCIndex = random. Next (7);//Random Color index values intFindex = random. Next (5);//Random Font index valuesFont f =NewFont (Fonts[findex], the, FontStyle.Bold);//FontBrush B =NewSolidBrush (C[cindex]);//Color intII =4; if((i +1) %2==0)//Control Verification code not at the same height{II=2; } g.drawstring (Validatenum.substring (i,1), F, B,3+ (I * A), ii);//draw a validation character} MS=NewMemoryStream ();//generating a memory stream objectImg.save (MS, Imageformat.jpeg);//Save this image to a stream in the format of a PNG image file//Recycling ResourcesG.dispose (); Img.dispose (); returnMs. ToArray (); } }}
Call method, controller plus a method, HTML
Public actionresult Validatecode () { new Captcha (); var code = Captcha. Createvalidatenumber (4); HttpContext.Session.SetString ("validatecode", code); return " Image/jpeg " ); }
Migrating legacy systems to. Net Core 2.0 Diaries (13)--Graphics verification Code