Asp. NET how to generate graphics verification code

Source: Internet
Author: User
This article describes the method of generating a graphics verification code from ASP. Share to everyone for your reference, as follows:

Typically there are 3 steps to generating a graphical verification code:

(1) randomly produces a random string of length n, and the value of n can be set by the developer at the developer's discretion. The string can contain numbers, letters, and so on.
(2) The randomly generated string is created as a picture and displayed.
(3) Save the Verification code.

Create a new page for Default.aspx, place a TextBox control and an image control, the TextBox control is used to enter the generated string, the image control is used to display the string, and its picture is the generated graphics verification code imageurl= "/ Default.aspx ";

The source code for 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 code for the Graphics 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; }}//Production random number 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,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 into an array of string randomnum = "";               int temp =-1;    Record the value of the last random number, and 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 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+12,22);    Graphics g = graphics.fromimage (image);      try {//Generate random generator randomly random = new random ();      Empty picture background 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,x2,y1,y2); } Font font = new System.Drawing.Font ("Arial", and (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 the foreground noise point 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 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 (); }  }}

It is hoped that this article will help you with ASP.

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.