<%@ page language= "C #" autoeventwireup= "true" codefile= "Login.aspx.cs" inherits= "Login"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
//verifycode.aspx is the default generated code
Using System; Using System.Data; Using System.Configuration; Using System.Collections; Using System.Web; Using System.Web.Security; Using System.Web.UI; Using System.Web.UI.WebControls.WebParts; Using System.Web.UI.HtmlControls; Using System.Drawing; Using System.Drawing.Imaging; Using System.Drawing.Drawing2D; Using System.IO; Public partial class VerifyCode:System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {//generateva Lidatecode (); Generateverifyimage (4);//generateverifyimage (int length)} #region "No refresh imitation Google waveform distorted color" CAPTCHA style 0___generatevalidatecode () private void Generatevalidatecode () {this. Length = This.length; This. FontSize = this.fontsize; This. Chaos = This.chaos; This. BackgroundColor = This.backgroundcolor; This. Chaoscolor = This.chaoscolor; This. Codeserial = this.codeserial; This. Colors = this.colors; This. Fonts = this.fonts; This. Padding = this.padding; String vnum = this. Createverifycode (); Take the random code session["Validatecode"] = Vnum.toupper ();//obtain a verification code for later inspectionCertificate. Createimageonpage (Vnum, this. Context); Output picture//cookie authentication mode, use Cookies to get the value of the Captcha//response.cookies.add (new HttpCookie ("Checkcode", code. ToUpper ())); } #endregion #region The length of the CAPTCHA (default 4 captcha length) int length = 4; public int Length {get {return Length;} set {length = value;}} #endregion #region captcha font size (default 40 pixels, which can be modified in order to show the distortion effect) in t fontSize = 22; public int FontSize {get {return FontSize;} set {FontSize = value;}} #endregion #region border complement (default 1 pixels) int padding = 2; public int Padding {get {return Padding;} set {Padding = value;}} #endregion #region whether to output dryness point (default not output) bool Chaos = True ; public bool Chaos {get {return Chaos;} set {Chaos = value;}} #endregion #region output dryness (default gray) Color Chaoscolor = Col Or. Lightgray; Public color Chaoscolor {get {return chaoscolor;} set {chaoscolor = value;}} #endregion #region custom background color (default white) color B Ackgroundcolor = Color.White; Public Color BackgroundColor {get {return backgroundcolor;} set {backgroundcolor = value;}} #endregion #region CustomRandom color array color[] colors = {color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.darkcyan , color.purple}; Public color[] Colors {get {return Colors;} set {Colors = value;}} #endregion #region custom font array string[] fonts = {"Ar Ial "," Georgia "}; Public string[] Fonts {get {return Fonts;} set {Fonts = value;}} #endregion #region custom random code string sequence (separated by commas) string CodeS erial = "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,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 "; public string Codeserial {get {return codeserial;} set {codeserial = value;}} #endregion #region produce a waveform filter effect private CO NST double PI = 3.1415926535897932384626433832795; Private Const Double PI2 = 6.283185307179586476925286766559; <summary>//sine wave distorted picture///</summary>//<param name= "srcbmp" > Picture path </param>//< param name= "Bxdir" > If the Twist is selected as true</param>//<param name= "nmultvalue" > Amplitude multiples of the waveform, the greater the degree of distortion, the more generally 3</PAram>//<param name= "Dphase" > Waveform starting phase, value range [0-2*PI] </param>//<returns></returns> public Bitmap twistimage (Bitmap srcbmp, bool Bxdir, double dmultvalue, double dphase) {Bitmap destbmp = new Bitmap (Srcbmp.width, Srcbmp.height); Fills the bitmap background with white System.Drawing.Graphics graph = graphics.fromimage (destbmp); Graph. FillRectangle (New SolidBrush (Color.White), 0, 0, destbmp.width, destbmp.height); Graph. Dispose (); Double Dbaseaxislen = Bxdir? (double) Destbmp.height: (double) destbmp.width; for (int i = 0, i < destbmp.width; i++) {for (int j = 0; J < Destbmp.height; J + +) {Double dx = 0; dx = bxdir? (PI2 * (double) j)/Dbaseaxislen: (PI2 * (double) i)/Dbaseaxislen; DX + = Dphase; Double dy = math.sin (dx); Gets the color of the current point int noldx = 0, Noldy = 0; NOLDX = Bxdir? i + (int) (DY * dmultvalue): i; Noldy = Bxdir? J:j + (int) (DY * dmultvalue); System.Drawing.Color Color = Srcbmp.getpixel (i, j); if (noldx >= 0 && noldx < destbmp.width && Noldy ≫= 0 && Noldy < destbmp.height) {Destbmp.setpixel (NOLDX, noldy, color);}} } return destbmp; } #endregion #region generate a checksum picture public Bitmap Createimagecode (string code) {int fsize = FontSize; int fwidth = fsize + Paddi Ng int imagewidth = (int) (code. Length * fwidth) + 4 + Padding * 2; int imageheight = fsize * 2 + Padding; System.Drawing.Bitmap image = New System.Drawing.Bitmap (imagewidth, imageheight); Graphics g = graphics.fromimage (image); G.clear (BackgroundColor); Random rand = new Random (); Adds a randomly generated dry point if (this) to the background. Chaos) {Pen pen = new Pen (chaoscolor, 0), int c = Length *, for (int i = 0; i < C; i++) {int x = rand. Next (image. Width); int y = rand. Next (image. Height); G.drawrectangle (pen, x, Y, 1, 1); }} int left = 0, top = 0, Top1 = 1, top2 = 1; int n1 = (imageheight-fontsize-padding * 2); int n2 = N1/4; Top1 = n2; Top2 = n2 * 2; Font F; Brush b; int CIndex, Findex; Random font and color verification code characters for (int i = 0; i < code. Length; i++) {cindex = rand. Next (colors.length-1); Findex = Rand. Next (fonts.length-1); f = new System.Drawing.Font (Fonts[findex], fsize, System.Drawing.FontStyle.Bold); b = new System.Drawing.SolidBrush (Colors[cindex]); if (i% 2 = = 1) {top = TOP2;} else {top = TOP1;} left = i * fwidth; g.DrawString (code. Substring (i, 1), F, B, left, top); }//Draw a border border color of Color.gainsboro g.drawrectangle (new Pen (Color.gainsboro, 0), 0, 0, image. Width-1, image. HEIGHT-1); G.dispose (); Generate Waveform (ADD by 51aspx.com) image = Twistimage (image, True, 4, 4); return image; } #endregion #region to output the created picture to the page public void Createimageonpage (string code, HttpContext context) {Response.bufferoutput = true; Special attention is paid to Response.Cache.SetExpires (DateTime.Now.AddMilliseconds (-1));//Special attention to Response.Cache.SetCacheability ( Httpcacheability.nocache);//Special attention to Response.appendheader ("Pragma", "No-cache"); Special attention MemoryStream ms = new MemoryStream (); Bitmap image = this. Createimagecode (code); Image. Save (MS, Imageformat.jpeg); Response.clearcontent (); Response.ContentType = "ImagE/jpeg "; Response.BinaryWrite (Ms. ToArray ()); Response.End (); Ms. Close (); ms = NULL; Image. Dispose (); image = NULL; } #endregion #region generate random character code public string Createverifycode (int codelen) {if (Codelen = = 0) {Codelen = Length;} string[ ] arr = codeserial.split (', '); String code = ""; int randvalue =-1; Random rand = new Random (unchecked ((int) DateTime.Now.Ticks)); for (int i = 0; i < Codelen; i++) {randvalue = rand. Next (0, arr.) LENGTH-1); Code + = Arr[randvalue]; } return code; } public string Createverifycode () {return Createverifycode (0)} #endregion #region another captcha style generateverifyimage (int len GTH)////<summary>///will create a good picture output to page///</summary> public void generateverifyimage (int nlen) {string validate Code = "";//generated verification code int nbmpwidth = Getimagewidth (Nlen); int nbmpheight = Getimageheight (); System.Drawing.Bitmap bmp = New System.Drawing.Bitmap (nbmpwidth, nbmpheight); The image is bent Twistimage (BMP, True, 12, 2); 1. Generate random background color int nred, ngreen, Nblue; Tri-color System.ra backgroundNdom rd = new Random ((int) System.DateTime.Now.Ticks); nred = Rd. Next (255)% 128 + 128; Ngreen = Rd. Next (255)% 128 + 128; nblue = Rd. Next (255)% 128 + 128; 2. Fill bitmap background System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage (BMP); Graph. FillRectangle (New SolidBrush (System.Drawing.Color.FromArgb (nred, Ngreen, Nblue)), 0, 0, nbmpwidth, nbmpheight); 3. Draw the interfering lines, using a slightly darker color than the background int nlines = 3; System.Drawing.Pen Pen = new System.Drawing.Pen (System.Drawing.Color.FromArgb (NRED-17, NGREEN-17, NBLUE-17), 2); for (int a = 0; a < Nlines; a++) {int x1 = Rd. Next ()% Nbmpwidth; int y1 = Rd. Next ()% Nbmpheight; int x2 = Rd. Next ()% Nbmpwidth; int y2 = Rd. Next ()% Nbmpheight; Graph. DrawLine (pen, x1, y1, x2, y2); }//The character set that can be expanded immediately, and can control the probability that the character will appear string strcode = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 4. Loop to get the character and draw for (int i = 0; i < Nlen; i++) {int x = (i * + rd). Next (3)); int y = Rd. Next (4) + 1; Determine font System.Drawing.Font font = new System.drAwing. Font ("Courier New",//Text font type + Rd. Next ()% 4,//text font size System.Drawing.FontStyle.Bold);//text font style char c = strcode[rd. Next (Strcode.length)]; Random get character Validatecode + = C.tostring (); Draws the character graph. DrawString (c.tostring (), Font, New SolidBrush (System.Drawing.Color.FromArgb (nRed-60 + y * 3, nGreen-60 + y * 3, Nblue -+ y * 3)), x, y); } session["Validatecode"] = Validatecode; The image is bent Twistimage (BMP, True, 4, 4); Response.bufferoutput = true; Special attention is paid to Response.Cache.SetExpires (DateTime.Now.AddMilliseconds (-1));//Special attention to Response.Cache.SetCacheability ( Httpcacheability.nocache);//Special attention to Response.appendheader ("Pragma", "No-cache"); Special note//5. Output byte stream MemoryStream bstream = new MemoryStream (); Bmp. Save (Bstream, imageformat.jpeg); Response.clearcontent (); Response.ContentType = "Image/jpeg"; Response.BinaryWrite (Bstream. ToArray ()); Response.End (); Bstream. Close (); Bstream = null; Bmp. Dispose (); BMP = NULL; Graph. Dispose (); }///<summary>///Get the width of the captcha picture///</summary>///<paramName= "Validatenumlength" > Length of verification code </param>///<returns></returns> public static int Getimagewidth (int validatenumlength) {return (int) (* validatenumlength + 5);} <summary>//The height of the verification code///</summary>///<returns></returns> public static int Getimageheight () {return;} #endregion}