ASP. NET verification code implementation (source code included), asp.net source code

Source: Internet
Author: User

ASP. NET verification code implementation (source code included), asp.net source code

First, let's take a look at the effect implementation (because the gif screen recording software is instantly found, some of them are missing)

The Code mainly refers to the implementation of the verification code class.

Using System; using System. collections. generic; using System. linq; using System. web; using System. drawing; using System. IO; namespace SecurityCodePic {public class DrawingSecurityCode {// <summary> // generate the verification code, and returns /// </summary> /// <returns> </returns> public string GetSecurityCode (int n) {string code = GenerateCheckCode (n); CreateCheckCodeImage (code ); return code ;} /// <summary> /// dynamically generate a specified number of random numbers or letters /// </summary> /// <param name = "num"> integer </param> /// <returns> return verification code string </returns> private string GenerateCheckCode (int num) {int number; // defines the variable char code; string checkCode = String. empty; // Empty string, read-only Random random = new Random (); // defines the Random variable instance for (int I = 0; I <num; I ++) {// use the for loop to generate a specified number of random numbers or letters number = random. next (); // return a non-negative random number less than the specified maximum value. next has three constructors if (number % 2 = 0) {// generate a single-digit code = (char) ('0' + (char) (number % 10 ));} else {// generate an uppercase letter code = (char) ('A' + (char) (number % 26);} checkCode + = code. toString () ;}return checkCode ;} /// <summary> /// generate a verification code Image Based on the verification code string /// </summary> /// <param name = "checkCode"> Verification code string </param> private void CreateCheckCodeImage (string checkCode) {if (checkCode = null | checkCode. trim () = String. empty) return; // reference System. drawing Class Library Bitmap myImage = new Bitmap (80, 30); // generate a specified size Bitmap Graphics graphics = Graphics. fromImage (myImage); // generate a canvas from a bitmap try {graphics. clear (Color. white); // clear the entire painting surface and fill it with the specified background color. Here, the background color is set to White Random random = new Random (); // instantiate a pseudo-random number generator // foreground noise of the image. Here there are 100 for (int I = 0; I <100; I ++) {int x = random. next (myImage. width); int y = random. next (myImage. height); myImage. setPixel (x, y, Color. fromArgb (random. next (); // specify the color of the pixel at x and y in the coordinate field} // draws the background noise line of the image. Here, there are 2 for (int I = 0; I <2; I ++) {int x1 = random. next (myImage. width); int x2 = random. next (myImage. width); int y1 = random. next (myImage. height); int y2 = random. next (myImage. height); graphics. drawLine (new Pen (Color. black), x1, y1, x2, y2); // draw a line of the specified color from x1, y1 to x2, y2, the lines here are black} Font font = new Font ("Arial", 15, FontStyle. bold); // defines the specific text format. The font here is Arial and the size is 15, bold font // generate a LinearGradientBrush instance based on the rectangle, start color, end color, and direction angle --- linear gradient System. drawing. drawing2D. linearGradientBrush brush = new System. drawing. drawing2D. linearGradientBrush (new Rectangle (0, 0, myImage. width, myImage. height), // instantiate a rectangle Color of the same size as myImage at coordinates 0, 0. blue, Color. red, 1.2f, true); // draw the text string graphics. drawString (checkCode, font, brush, 2, 2); // draw a rectangle with coordinate pairs, width, and height --- draw the border line graphics of the image. drawRectangle (new Pen (Color. silver), 0, 0, myImage. width-1, myImage. height-1); // create it to support memory for memory stream MemoryStream MS = new MemoryStream (); // save this image to the specified stream in the specified format myImage. save (MS, System. drawing. imaging. imageFormat. gif); // save it to the memory HttpContext in gif format. current. response. clearContent (); // clear all content in the buffer stream and output HttpContext. current. response. contentType = "image/Gif"; // obtain or set the http mime type of the output stream HttpContext. current. response. binaryWrite (ms. toArray (); // write a binary string to the HTTP output stream} finally {// release resources occupied by graphics. dispose (); myImage. dispose ();}}}}


Then, use the SecurityCode. ashx file to call the method implementation of the above class.

Using System; using System. collections. generic; using System. linq; using System. web; namespace SecurityCodePic {// <summary> // summary of SecurityCode1 /// </summary> public class SecurityCode1: IHttpHandler {public void ProcessRequest (HttpContext context) {DrawingSecurityCode SC = new DrawingSecurityCode (); string SecurityCode = SC. getSecurityCode (6);} public bool IsReusable {get {return false ;}}}}


Finally, the reference of the image path on the ASP. NET page is provided.

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "SecurityCode_Test.aspx.cs" Inherits = "SecurityCodePic. SecurityCode_Test" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

The above is all about this article. By the way, you can download and share the source code. You are welcome to download it.

Source code sharing: ASP. NET verification code implementation

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.