MVC Series Learning (15)-Verification code

Source: Internet
Author: User

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

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.