1. Mode one:
Public classvcode{/// <summary> ///generate a Captcha picture byte array/// </summary> /// <returns></returns> Public byte[] Getvcode () {using(Image img =NewBitmap ( -, -)) { stringStrcode =Getrandomstr (); httpcontext.current.session["Vcode"] =Strcode; using(Graphics g =graphics.fromimage (IMG)) {g.clear (color.white); G.drawrectangle (Pens.blue,0,0Img. Width-1Img. Height-1); Drawpoint (g); g.DrawString (Strcode,NewFont ("Microsoft Ya-Black", the), Brushes.blue,NewPointF (5,2)); Drawpoint (g); using(System.IO.MemoryStream ms =NewMemoryStream ()) { //to save a picture to the memory streamimg. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg); //converts the data in the memory stream into a byte array to return returnMs. ToArray (); } } } return NULL; } Random Random=NewRandom (); /// <summary> ///Generate random numbers/// </summary> /// <returns></returns> stringGetrandomstr () {stringstr =string. Empty; string[] Strarr = {"again","N","B","of the","Dream","want to","also","Arrive","No","ever","S","B","a","Sample","of the","Jian","hold"}; for(inti =0; I <4; i++) { intindex =Random. Next (strarr.length); STR+=Strarr[index]; } returnstr; } /// <summary> ///Draw line Segments/// </summary> /// <param name= "G" ></param> voidDrawpoint (Graphics g) {pen[] pens={pens.blue, Pens.black, pens.red, pens.green}; Point P1; Point P2; intLength =1; for(inti =0; I < -; i++) {P1=NewPoint (random. Next ( -), Random. Next ( in)); P2=NewPoint (P1. X-length, p1. Ylength); Length= Random. Next (5); G.drawline (Pens[random. Next (pens. Length)], p1, p2); } }}
Call:
Way two:
Using generic Handlers
Validatecode.ashx Download
Call
3. Draw conclusions
It is also possible to use a generic handler in MVC, because MVC and WebForm use the same day ASP. When the request arrives at the request pipeline, in the seventh event, see if the request implements the Mvchandler interface, if not, skip the seventh event, go to the eighth event, create a generic handler object in the 8th event, and then call the object's PR () method between the 11th and 12 events
MVC Series Learning (15)-Verification code