ASP. Generate mixed-letter and digital graphics verification Code

Source: Internet
Author: User
The implementation method is as follows:

1, the main idea is: reference using the System.Drawing namespace, using the graphics FromImage method to create a canvas, while setting the width and height of the canvas, and then through the graphics class The drawstring method randomly generates a string to draw into the canvas, while drawing a verification code, drawing some color points in the canvas using the SetPixel method, thus preventing illegal people from using robots for landing. After we have drawn the verification code, in the page that needs the verification code to display it with the image space, the image control shows the code of the HTML 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 not see Clearly, change a </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 sender, EventArgs e) {if (!      IsPostBack) {String validatenum = Createrandomnum (4);//Genetic 4-bit random string createimage (validatenum);//Draw random strings generated as pictures session["Validatenum"] = validatenum;//Save Verification Code}}//Generate random strings private string createrandomnum (int numcount) {str    ing Allchar = "0,1,2,3,4,5,6,7,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 into an array of string randomnum = "";    int temp = -1;//records The value of the last random number, trying to avoid generating several identical random numbers for the randomness 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 picture private void CreateImage (string validatenum) {if (Validatenum = = NULL | | Validatenum.trim () = = String.empt    Y) return;    Generate Bitmap images System.Drawing.Bitmap image = new System.Drawing.Bitmap (validatenum.length * 12 + 10, 22);     Graphics g = graphics.fromimage (image);       try {//Generate random generator randomly random = new random ();       Empty the picture background color g.clear (color.white); Picture the background noise line for (int i = 0; i < 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",Stem. 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); Picture the foreground noise point 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 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 (); Saves 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 about the ASP. NET generated graphics verification code related Materials, I hope that everyone's learning is helpful.

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.