<% @ WebHandler Language = "C #" Class = "CreateCode" %>
Using System; Using System. Web; Using System. Drawing; Using System. Drawing. Imaging; Using System. Web. SessionState; Using System. Collections. Generic; Using System. Text; Using System. Drawing;
Public class CreateCode: IHttpHandler, IRequiresSessionState { /// <Summary> /// Dynamic verification code by Jack /// Calculate the verification code when the value type is not null /// </Summary> Public void ProcessRequest (HttpContext context) { System. Web. HttpResponse response = context. Response; // Text content of the Verification Code String type = ""; If (context. Request ["type"]! = Null) Type = context. Request ["type"]. ToString (); String checkCode = ""; If (string. IsNullOrEmpty (type )) { CheckCode = CreateCheckCodeString (); Context. Session. Add ("gs_codeStr", checkCode ); } Else { String [] codes = CreateCheckCode (); CheckCode = codes [0]; Context. Session. Add ("gs_codeStr", codes [1]); } Int width = 80; // The image width of the verification code. Int height = 22; // The Image height of the Verification Code Font font = new Font ("Arial", 12, FontStyle. Bold); // the Font of the verification code. SolidBrush brush = new SolidBrush (Color. Black); // The brush used to write the verification code Pen crosswise = new Pen (Color. Green, 0); // draw a Pen with horizontal interference lines Pen vertical = new Pen (Color. FromArgb (255,100,100,100), 0); // draw a Pen with vertical interference lines Bitmap image = new Bitmap (width, height); // generate an image Graphics g = Graphics. FromImage (image); // generate a painting Panel (canvas) G. Clear (ColorTranslator. FromHtml ("# f0f0f0"); // fill the canvas with the specified color RectangleF rect = new RectangleF (10, 2, width, height); // define the rectangle for text painting floating x, y, width, height, Random rand = new Random (int) DateTime. Now. Ticks); // generate Random object of interference line For (int I = 0; I <2; I ++) { Point start = new Point (0, rand. Next (height )); Point end = new Point (width, rand. Next (height )); G. DrawLine (crosswise, start, end ); } For (int I = 0; I <4; I ++) { Point start = new Point (rand. Next (width), 0 ); Point end = new Point (rand. Next (width), height ); G. DrawLine (vertical, start, end ); } G. DrawString (checkCode, font, brush, rect); // write the verification code to the canvas. System. IO. MemoryStream MS = new System. IO. MemoryStream (); Try { Image. Save (MS, System. Drawing. Imaging. ImageFormat. Gif ); Response. ClearContent (); Response. ContentType = "image/Gif "; Response. BinaryWrite (ms. ToArray ()); } Finally { Ms. Flush (); Ms. Close (); Ms. Dispose (); G. Dispose (); Image. Dispose (); } } // Define the string Verification Code Private string CreateCheckCodeString () {// Define the character array used for verification code Char [] AllCheckCodeArray = {'1', '2', '3', '4', '5', '6', '7', '8 ', '9', 'A', 'B', 'C ', 'D', 'E', 'F', 'G', 'h', 'I', 'J', 'k', 'l', 'M ', 'N', 'O', 'P', 'Q', 'R', 's', 't', 'U', 'V', 'w ', 'X', 'y', 'z '}; // Define the verification code string String randomcode = ""; Random rd = new Random (); // Generate a four-digit verification code string For (int I = 0; I <5; I ++) Randomcode + = AllCheckCodeArray [rd. Next (AllCheckCodeArray. Length)]; Return randomcode; }
// Define the number operation verification code {1 + 1 =? , 2} Private string [] CreateCheckCode () {// Define the character array used for verification code Int [] AllCheckCodeArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // Define the verification code string String randomcode = ""; // 1 + 1 =? Int result = 0; // 2 Random rd = new Random (); // Generate a four-digit verification code string For (int I = 0; I <5; I ++) If (I! = 1 & I! = 4) { Int curint = AllCheckCodeArray [rd. Next (AllCheckCodeArray. Length)]; If (I = 0 | I = 2) Result + = curint; If (I = 3) Randomcode + = "= "; Else Randomcode + = curint; } Else { If (I = 1) Randomcode + = "+ "; If (I = 4) Randomcode + = "? "; } Return new string [] {randomcode, result. ToString ()}; }
Public bool IsReusable { Get { Return false; } }
} |