ASP-Generate verification code

Source: Internet
Author: User
Tags border color

Final effect:

Working with files:

Verifycode.aspx

Front verifycode.aspx:

    • Again Page_Load in the code, is how to generate, as well as create session.
    • int length = 6; Change the number of characters in the generated verification code.
    • int fontSize = ; Changes the size of the generated font, in pixels.
Using system;using system.drawing;using System.web;public partial class verifycode:system.web.ui.page{protected void        Page_Load (object sender, EventArgs e) {Validatedcode v = new Validatedcode ();            String code = V.createverifycode (); Take the Random Code v.createimageonpage (code, this.       Context);                   Output image session["Checkcode"] = code;        Create session} public class Validatedcode {#region Verification code length (default 6 captcha length) int length = 6;            public int Length {get {return length;            } set {length = value;        }} #endregion #region Captcha font size (to show distortions, default 40 pixels, you can modify it yourself) int fontSize = 50;            public int FontSize {get {return FontSize;            } set {fontSize = value; }} #endregion #regionBorder complement (default 1 pixels) int padding = 2;            public int Padding {get {return Padding;            } set {padding = value;        }} #endregion #region whether to output the dryness point (the default is not output) bool Chaos = true;            public bool Chaos {get {return Chaos;            } set {chaos = value;        }} #endregion #region the color of the output dryness point (default gray) Chaoscolor = Color.lightgray;            Public Color Chaoscolor {get {return chaoscolor;            } set {Chaoscolor = value;        }} #endregion #region custom background color (default white) color backgroundcolor = Color.White;            Public Color BackgroundColor {get {return backgroundcolor; } set            {backgroundcolor = value; }} #endregion #region custom random color array color[] colors = {color.black, color.red, Color.darkblue, Co Lor.        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 = {"Arial", "Georgia"};            Public string[] Fonts {get {return Fonts;            } set {fonts = value; }} #endregion #region a sequence of custom random code strings (delimited with commas) string codeserial = "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 Const double PI = 3.1415926535897932384626433832795;        Private Const Double PI2 = 6.283185307179586476925286766559; <summary>//sine wave distorted picture (Edit by 51aspx.com)///</summary>//<param Nam E= "srcbmp" > Picture path </param>//<param name= "Bxdir" > If Twist is selected as true</param>//<param Name= "Nmultvalue" > Waveform amplitude multiples, the greater the degree of distortion, generally 3</param>//<param name= "Dphase" > Waveform starting phase, the value range [0-2*PI] < /param>//<returns></returns> public System.Drawing.Bitmap twistimage (Bitmap srcbmp, bo Ol Bxdir, double dmultvalue, double dphase) {System.Drawing.Bitmap destbmp = new Bitmap (Srcbmp.width, S            Rcbmp.height); Fills the bitmap background with a white System.Drawing.GraphICS graph = System.Drawing.Graphics.FromImage (destbmp); Graph.            FillRectangle (New SolidBrush (System.Drawing.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 + Padding; 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 * 10;               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, 8, 4);        return image;        } #endregion #region Create a good picture output to page public void Createimageonpage (string code, HttpContext context)            {System.IO.MemoryStream ms = new System.IO.MemoryStream (); Bitmap image = this.            Createimagecode (code); Image.            Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg); Context.            Response.clearcontent (); Context.            Response.ContentType = "Image/jpeg"; Context. Response.BinaryWrite (Ms.            GetBuffer ()); Ms.            Close ();            ms = NULL; Image.            Dispose ();        image = NULL; #endregion #region generate random character codes        public string Createverifycode (int codelen) {if (Codelen = = 0) {Code            Len = 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}}

  

Background code:

original build, no modification can be.

Use:

  

ASP-Generate verification code

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.