Asp.net generates a hybrid graphic verification code for letters and numbers, and an asp.net Verification Code

Source: Internet
Author: User

Asp.net generates a hybrid graphic verification code for letters and numbers, and an asp.net Verification Code

Verification code is an important technology in website development. It can prevent unauthorized users from using registration machines or login tools to attack our website. Below is:

The specific implementation method is as follows:

1. Main IdeasYes: reference Using System. the Drawing namespace uses the FromImage method of Graphics to create a canvas, set the width and height of the canvas, and then draw a string randomly generated by the DrawString method of the Graphics class to the canvas, while drawing the verification code, you can use the SetPixel method to draw some color points on the canvas to prevent unauthorized users from logging on to the canvas. After the verification code is drawn, use the Image space to display it on the page where the verification code is required. The HTML source code for the Image control to display the verification code is set as follows:

<Asp: Image ID = "Image1" runat = "server" ImageUrl = "~ /ValidateNum. aspx "/> <asp: LinkButton ID =" LinkButton1 "runat =" server "style =" font-size: small; "> can't see clearly, change one </asp: LinkButton>

2. The ValidateNum. aspx page code used here is as follows:

Using System; using System. collections; 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 ValidateNum: System. web. UI. page {protected void Page_Load (object s Ender, EventArgs e) {if (! IsPostBack) {string validateNum = CreateRandomNum (4); // generate four random strings CreateImage (validateNum ); // map the generated random string to the image Session ["ValidateNum"] = validateNum; // Save the verification code} // generate the random string 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, L, m, N, O, P, Q, R, S, T, U, W, X, Y, Z "; string [] allCharArray = allChar. split (','); // Split it into an array string randomNum = ""; int temp =-1; // records Numeric value. 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 ;} // generate the 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 + 10, 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 (validateNum, 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 (); // 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 ();}}}

The above is the relevant information about the graphic verification code generated by asp.net. I hope it will be helpful for your learning.

Articles you may be interested in:
  • ASP.net verification code implementation (C #)
  • Asp.net (C #) implements the verification code Function
  • Asp.net verification code generation, refresh, and verification
  • Encapsulated asp.net Verification Code Class
  • Asp.net ajax implements no-refreshing Verification Code
  • HtmlHelper of asp.net image Verification Code
  • Asp.net generate Verification Code (only numbers)
  • Three verification code examples (Implementation Code) in asp.net (mixed numbers, numbers, letters, and Chinese characters)
  • ASP. net mvc Verification Code
  • A small example of ASP. NET verification code implementation and refresh Verification Code

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.