Detailed explanation of how ASP. NET generates a graphic verification code, asp.net Verification Code

Source: Internet
Author: User

Detailed explanation of how ASP. NET generates a graphic verification code, asp.net Verification Code

This document describes how to generate a graphic verification code using ASP. NET. We will share this with you for your reference. The details are as follows:

Generally, there are three steps to generate a graphic verification code:

(1) randomly generate a random string with a length of N. The value of N can be set by developers. This string can contain numbers, letters, and so on.
(2) create a randomly generated string as an image and display it.
(3) Save the verification code.

Create a new page as default. aspx: place a TextBox Control and an Image control. The TextBox Control is used to input the generated string. The Image control is used to display the string. The Image is the generated Image Verification Code imageUrl = "/default. aspx ";

The source code of the default. aspx page is:

<form id="form1" runat="server">  <div>    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>    <asp:Image ID="Image1" imageUrl=“/default.aspx” runat="server" />  </div></form>

The graphic verification code is:

Using System; using System. configuration; using System. data; using System. linq; using System. web; using System. web. security; using System. web. UI; using System. web. UI. htmlControls; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. xml. linq; using System. drawing; public partial class _ Default: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {string validateNum = CreateRandomNum (4); CreateImage (validateNum); Session ["ValidateNum"] = validateNum;} // generate a random number private string CreateRandomNum (int NumCount) {string allChar = "0, 1, 2, 3, 4, 5, 6, 8, 9, A, B, C, D, E, F, G, H, I, J, K, O, p, Q, R, S, T, U, W, X, Y, Z, a, B, c, d, e, f, g, h, I, j, k, m, n, o, p, q, s, t, u, w, x, y, z "; string [] allCharArray = allChar. split (','); // Split it into an array string randomNum = ""; int temp =-1; // record The value of the last Random number. Try to avoid generating several identical Random numbers: Random rand = new Random (); for (int I = 0; I <NumCount; I ++) {if (temp! =-1) {rand = new Random (I * temp * (int) DateTime. now. ticks);} int t = rand. next (35); if (temp = t) {return CreateRandomNum (NumCount);} temp = t; randomNum + = allCharArray [t];} return randomNum ;} // production image private void CreateImage (string validateNum) {if (validateNum = null | validateNum. trim () = string. empty) return; // generate BitMap image System. drawing. bitmap image = new System. drawing. bitmap (validateNum. length * 12 + 12, 22); Graphics g = Graphics. fromImage (image); try {// generate Random generator random Random = new Random (); // clear the image background 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, x2, y1, 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 (validateNum, font, brush, 100); // foreground noise of the image to be painted for (int I = 0; I <; 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 (); // Save the image to the specified stream image. save (MS, System. drawing. imaging. imageFormat. gif); Response. clearContent (); Response. contentType = "image/Gif"; Response. binaryWrite (ms. toArray ();} finally {g. dispose (); image. dispose ();}}}

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.