MVC series Learning (15th)-verification code and mvc series learning Verification Code

Source: Internet
Author: User

MVC series Learning (15th)-verification code and mvc series learning Verification Code

1. Method 1:

Public class VCode {// <summary> /// generate a verification code image byte array /// </summary> /// <returns> </returns> public byte [] GetVCode () {using (Image img = new Bitmap (90, 30) {string strCode = GetRandomStr (); HttpContext. current. session ["vcode"] = strCode; using (Graphics g = Graphics. fromImage (img) {g. clear (Color. white); g. drawRectangle (Pens. blue, 0, 0, img. width-1, img. height-1); DrawPoint (g); g. drawString (strCode, new Font ("", 15), Brushes. blue, new PointF (5, 2); DrawPoint (g); using (System. IO. memoryStream MS = new MemoryStream () {// Save the image to the memory stream img. save (MS, System. drawing. imaging. imageFormat. jpeg); // convert the data in the memory stream to a byte array and return ms. toArray () ;}}return null;} Random random = new Random (); /// <summary> /// generate a random number /// </summary> /// <returns> </returns> string GetRandomStr () {string str = string. empty; string [] strArr = {"then", "N", "B", "of", "dream", "think", "also", "Arrival ", "No", "pass", "S", "B", "1", "sample", "," strong "," hold "}; for (int I = 0; I <4; I ++) {int index = random. next (strArr. length); str + = strArr [index];} return str ;} /// <summary> // draw a line segment /// </summary> /// <param name = "g"> </param> void DrawPoint (Graphics g) {Pen [] pens = {Pens. blue, Pens. black, Pens. red, Pens. green}; Point p1; Point p2; int length = 1; for (int I = 0; I <50; I ++) {p1 = new Point (random. next (79), random. next (29); p2 = new Point (p1.X-length, p1.Y-length); length = random. next (5); g. drawLine (pens [random. next (pens. length)], p1, p2 );}}}

Call:

Method 2:

Use a general Handler

Download ValidateCode. ashx

Call

3. Draw conclusions

In the past, general processing programs can also be used in mvc, because mvc and webform use the same day asp.net framework. When a request arrives in the request pipeline, check whether the mvchandler interface is implemented in the request in the seventh event. If not, skip the seventh event and enter the eighth event. Among the 8th events, create a general handler object, and then call the object's PR () method between 11th and 12 events.

Related Article

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.