Generate verification code C # code implementation

Source: Internet
Author: User

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 ;}}}

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.