Simple ASP. NET verification code and asp.net Verification Code
Today I wrote a simple verification code implementation. Paste the code.
Protected void Page_Load (object sender, EventArgs e) {CreateCheckCodeImage (RndNum ();} private string RndNum () {int number; char code; string checkCode = String. empty; System. random random = new Random (); for (int I = 0; I <4; I ++) {number = random. next (); if (number % 2 = 0) code = (char) ('0' + (char) (number % 10); else code = (char) ('A' + (char) (number % 26); checkCode + = code. toString ();} Response. cookies. add (new HttpCookie ("yzmcode", checkCode); return checkCode;} private void CreateCheckCodeImage (string checkCode) {if (checkCode = null | checkCode. trim () = String. empty) return; System. drawing. bitmap image = new System. drawing. bitmap (int) Math. ceiling (checkCode. length * 12.5), 22); Graphics g = Graphics. fromImage (image); try {// generate Random generator random Random = new Random (); // clear the image background color g. clear (Color. white); // specifies the background noise line of the image. for (int I = 0; I <25; I ++) {int x1 = random. next (image. width); int x2 = random. next (image. width); int y1 = random. next (image. height); int y2 = random. next (image. height); g. drawLine (new Pen (Color. silver), x1, y1, x2, y2);} Font font = new System. drawing. font ("Arial", 12, (System. drawing. fontStyle. bold | System. drawing. fontStyle. italic); System. drawing. drawing2D. linearGradientBrush brush = new System. drawing. drawing2D. linearGradientBrush (new Rectangle (0, 0, image. width, image. height), Color. blue, Color. darkRed, 1.2f, true); g. drawString (checkCode, font, brush, 2, 2); // foreground noise of the image to be painted for (int I = 0; I <100; I ++) {int x = random. next (image. width); int y = random. next (image. height); image. setPixel (x, y, Color. fromArgb (random. next ();} // draw the border line g of the image. drawRectangle (new Pen (Color. silver), 0, 0, image. width-1, image. height-1); System. IO. memoryStream MS = new System. IO. memoryStream (); image. save (MS, System. drawing. imaging. imageFormat. gif); Response. clearContent (); Response. contentType = "image/Gif"; Response. binaryWrite (ms. toArray ();} finally {g. dispose (); image. dispose ();}}
The reference Page code is as follows:
"style =" cursor: pointer; "alt =" click to refresh "onclick =" Show (this); ">