HTML code:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <HTML>
C # code
<% @ Webhandler Language = "C #" class = "watermark" %> using system; using system. web; using system. drawing; using system. drawing. drawing2d; using system. web. sessionstate; public class watermark: ihttphandler, irequiressessionstate // You must implement this interface to use session. Remember to import system. web. sessionstate namespace {public void processrequest (httpcontext context) {string checkcode = gencode (5); // generates a five-character random context. session ["code"] = checkcode; // Save the string to the session so that system can be verified as needed. drawing. bitmap image = new system. drawing. bitmap (70, 22); graphics G = graphics. fromimage (image); try {// generate random generator random = new random (); // clear the image background color G. clear (color. white); // specifies the background noise line of the image. Int I; for (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. 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); // The foreground noise of the picture. G. 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); context. response. clearcontent (); context. response. contenttype = "image/GIF"; context. response. binarywrite (Ms. toarray ();} finally {G. dispose (); image. dispose ();}} /// <summary> /// generate a random string /// </Summary> /// <Param name = "num"> random output of several characters </param> // /<returns> random string </returns> private string gencode (INT num) {string STR = "0123456789 abcdefghijklmnopqrstuvwxyz"; char [] chastr = Str. tochararray (); string code = ""; random RD = new random (); int I; for (I = 0; I <num; I ++) {// code + = source [Rd. next (0, source. length)]; code + = Str. substring (Rd. next (0, str. length), 1) ;}return code ;}public bool isreusable {get {return false ;}}}