Dynamic Verification Code Generation

Source: Internet
Author: User

Knowledge point:

1. Operations on Images

 

Problem:

Dynamic Verification Code Generation

 

Solution

1 <% @ webhandler Language = "C #" class = "validatecode" %> 2 3 using system; 4 using system. web; 5 using system. drawing; 6 using system. io; 7 8 public class validatecode: ihttphandler {9 10 public void processrequest (httpcontext context) {11 context. response. contenttype = "text/plain"; 12 // context. response. write ("Hello World"); 13 random r = new random (); 14 int n = R. next (values, 100000); 15 createvalidategraphic (N. tostring (), context ); 16} 17 /// <summary> 18 // picture of the Verification Code created 19 /// </Summary> 20 /// <Param name = "containspage"> to output page Object </param> 21 // <Param name = "validatenum"> Verification Code </param> 22 public void createvalidategraphic (string validatecode, httpcontext context) 23 {24 bitmap image = new Bitmap (INT) math. ceiling (validatecode. length * 12.0), 22); 25 graphics G = graphics. fromimage (image); 26 try27 {28 // generate random generator 29 random = new random (); 30 // clear the image background color 31G. clear (color. white); 32 // The interference line of the picture. 33 for (INT I = 0; I <25; I ++) 34 {35 int X1 = random. next (image. width); 36 int X2 = random. next (image. width); 37 int Y1 = random. next (image. height); 38 int y2 = random. next (image. height); 39G. drawline (new pen (color. silver), X1, Y1, X2, Y2); 40} 41 Font font = new font ("Arial", 12, (fontstyle. bold | fontstyle. italic); 42 system. drawing. drawing2d. lineargradientbrush brush = new system. drawing. drawing2d. lineargradientbrush (New rectangle (0, 0, image. width, image. height), 43 color. blue, color. darkred, 1.2f, true); 44G. drawstring (validatecode, Font, brush, 3, 2); 45 // foreground disturbance of the picture: 46 for (INT I = 0; I <100; I ++) 47 {48 int x = random. next (image. width); 49 int y = random. next (image. height); 50 image. setpixel (X, Y, color. fromargb (random. next (); 51} 52 // draw the border line of the image 53g. drawrectangle (new pen (color. silver), 0, 0, image. width-1, image. height-1); 54 // Save image data 55 memorystream stream = new memorystream (); 56 image. save (stream, system. drawing. imaging. imageformat. JPEG); 57 // output image stream 58 Context. response. clear (); 59 context. response. contenttype = "image/JPEG"; 60 context. response. binarywrite (stream. toarray (); 61} 62 finally63 {64g. dispose (); 65 image. dispose (); 66} 67} 68 69 public bool isreusable {70 get {71 return false; 72} 73} 74 75}
View code

 

Dynamic Verification Code Generation

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.