This article mainly for you to introduce a simple ASP. NET verification code, with a certain reference value, interested in small partners can refer to
The example of this article for you to share the specific code of the ASP, for your reference, the specific content as follows
I mainly see the interference line, a verification code inside if there is no interference line or anything, at least in the noise and random code layout to do something:
<summary>///Verification Code generation class////</summary> public class Verify_code:ihttphandler, IRequiresSessionState {Pub LIC void ProcessRequest (HttpContext context) {int codew = 80; int Codeh = 22; int fontSize = 16; String Chkcode = String. Empty; Color list, for captcha, noise, noise color[] color = {Color.Black, color.red, Color.Blue, Color.green, Color.orange, Color.brown, color. Brown, Color.darkblue}; Font list, used for captcha string[] Font = {"Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact"}; Verification code of the character set, removed some easily confusing characters char[] character = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 8 ', ' 9 '}; Random rnd = new Random (); Generate a CAPTCHA string for (int i = 0; i < 4; i++) {Chkcode + = Character[rnd. Next (character. Length)]; }//write session context. session["Sys_verify_code"] = Chkcode; Create canvas Bitmap bmp = new Bitmap (Codew, Codeh); Graphics g = graphics.fromimage (BMP); G.clear (Color.White); Draw a noise line for (int i = 0; i < 4; i++) {int x1 = rnd. Next (Codew); int Y1 = rnd. Next (Codeh); int x2 = rnd. Next (Codew); int y2 = rnd. Next (Codeh); Color clr = color[rnd. Next (color. Length)]; G.drawline (new Pen (CLR), x1, y1, x2, y2); }//Draw the CAPTCHA string for (int i = 0; i < chkcode.length; i++) {string fnt = Font[rnd. Next (font. Length)]; Font ft = new Font (FNT, fontSize); Color clr = color[rnd. Next (color. Length)]; g.DrawString (Chkcode[i]. ToString (), FT, new SolidBrush (CLR), (float) i * + 2, (float) 0); }//Draw noise for (int i = 0; i < i++) {int x = rnd. Next (BMP. Width); int y = rnd. Next (BMP. Height); Color clr = color[rnd. Next (color. Length)]; Bmp. SetPixel (x, Y, CLR); }//Clears the page output cache and sets the page without caching context. Response.Buffer = true; Context. Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds (0); Context. Response.Expires = 0; Context. Response.CacheControl = "No-cache"; Context. Response.appendheader ("Pragma", "No-cache"); Writes the CAPTCHA picture to the memory stream and outputs it in "image/png" format MemoryStream ms = new MemoryStream (); try {bmp. Save (MS, Imageformat.png); Context. Response.clearcontent (); Context. Response.ContentType = "Image/png"; Context. Response.BinaryWrite (Ms. ToArray ()); finally {//explicitly releases the resource BMP. Dispose (); G.dispose (); }} public bool IsReusable {get {return false; } } }
Basic validation Generate code Demo:
Using system;using system.drawing;using system.drawing.imaging;using system.io;using System.Web;public partial class image:system.web.ui.page{protected void Page_Load (object sender, EventArgs e) {string tmp = Rndnum (4); HttpCookie a = new HttpCookie ("Imagev", TMP); Response.Cookies.Add (a); This. Validatecode (TMP); } private void Validatecode (String vnum) {Bitmap Img = null; Graphics g = null; MemoryStream ms = NULL; int gheight = vnum.length * 12; IMG = new Bitmap (gheight, 25); g = Graphics.fromimage (IMG); Background color g.clear (color.white); Text font f = new Font ("Arial Black", 10); Text color SolidBrush s = new SolidBrush (color.black); g.DrawString (Vnum, F, S, 3, 3); ms = new MemoryStream (); Img.save (MS, Imageformat.jpeg); Response.clearcontent (); Response.ContentType = "Image/jpeg"; Response.BinaryWrite (Ms. ToArray ()); G.dispose (); Img.dispose (); Response.End (); } private string Rndnum (int vcodenum) {string Vchar = "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,v,w,x,y,z "; string[] Vcarray = Vchar.split (new char[] {', '}); String vnum = ""; int temp =-1; Random rand = new Random (); for (int i = 1; i < Vcodenum + 1; i++) {if (temp! =-1) {rand = new Random (i * temp * unchecked ((int) DateTime . Now.ticks)); } int t = rand. Next (35); if (temp! =-1 && temp = = t) {return rndnum (vcodenum); } temp = t; Vnum + = vcarray[t]; } return vnum; }}