asp.net generate letters and numbers mixed graphics verification Code _ practical tips

Source: Internet
Author: User
Tags mixed rand

The Verification code technology is the important technology in the website development process, may prevent the illegal personnel to use the registration machine or the landing tool to attack our website. Here is the effect chart:

The implementation methods are as follows:

1, the main idea is: Reference using 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 DrawString methods randomly generated strings are drawn into the canvas, while drawing the captcha, the SetPixel method is used to draw some color points in the canvas to prevent illegal personnel from using the robot for landing. When we draw the verification code completed, in the need to verify the code of the page to use the image space to display it, the image control display verification code HTML source settings are 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 piece </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);//Create a random string of characters as a picture
  session["Validatenum"] = validatenum;//Save Authenticode}//Generate random string private string createrandomnum (int numcount)
    {String 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 array string randomnum = "";
    int temp = -1;//records the number of the last random number and tries 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 Picture private void CreateImage (string validatenum) {if (Validatenum = null | | Validatenum.trim () = string.
    Empty) return;
    Generate Bitmap images System.Drawing.Bitmap image = new System.Drawing.Bitmap (validatenum.length * 12 + 10, 22);

    Graphics g = graphics.fromimage (image);

      try {//Generate a random generator Random Random = new Random ();

      Clear the picture background color g.clear (color.white); The background noise line for the picture is 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), (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); Picture 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 picture's border line 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 ASP.net generate graphics verification Code of the relevant information, I hope to help you learn.

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.