Simple ASP. NET verification code and asp.net Verification Code

Source: Internet
Author: User

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); ">

 

Related Article

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.