.net handler generates a random 6-bit picture verification code, we refer to the use of the bar
The code is as follows:///<summary> ///PicHandler1 Summary description ///</summary> public CLA SS Pichandler1:ihttphandler, IRequiresSessionState { private string mcheckno = String. Empty; protected Imgbuilder _imgbuilder = new Imgbuilder (); protected Vryimggen _imgbuildernew = new Vryimggen (); private String _text = String. Empty; private String _font = "Song Body"; private int _fontsize = 8; private int _padding = 2; public void ProcessRequest (HttpContext context) { &N Bsp Mcheckno = Discheckno (); context. session["Checkcode"] = Mcheckno; this._imgbuilder.fontsize = this._fontsize; this._imgbuilder.padding = this._padding; if (!string. IsNullOrEmpty (This._font)) { &N Bsp this._imgbuilder.fonts = new string[] {This._font}; this._imgbuildernew.chaoswight = 40; this._imgbuildernew.fontsize = 25; this._imgbuildernew.padding = 3; SYSTEM.DRAWING.BITMAP image = th Is._imgbuildernew.createimage (Mcheckno); System.IO.MemoryStream ms = new System.IO.MemoryStream (); image. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg); context. Response.clearcontent (); context. Response.ContentType = "Image/jpeg"; context. Response.BinaryWrite (Ms. ToArray ()); context. session["Checkcode"] = mcheckno.tostring (); If IRequiresSessionState is not implemented, there will be an error and cannot generate pictures context. Response.End (); } //CAPTCHA code generation protected string DisC Heckno () { String hash = Hashcode.getnext (); String checkno = string. Empty; Random rd = new Random (DateTime.Now.Millisecond); for (int i = 0; i < 6; i++) {  ; Checkno + = hash. Substring (Rd. Next (1, hash. Length-1), 1); Checkno = Checkno.replace ("0", RD. Next (1, 9). ToString ()); Checkno = Checkno.replace ("o", RD. Next (1, 9). ToString ()); Checkno = Checkno.replace ("O", RD. Next (1, 9). ToString ()); return checkno; } public bool isreusable {&nbs P get { &NBS P return false; } { } calling class The code is as follows: using System; Using System.Drawing; Using System.Text; ///<summary>///Vryimggen Summary description///</summary> PubLic class Vryimggen { public static string chinesechars = String.Empty; ///<summary > ///English with digital string ///</summary> protected static readonly string Englishorn Umchars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; Public Vryimggen () { rnd = new Random (unchecked (int) Dateti Me. Now.ticks)); } ///<summary> ///global random number generator ///</summary> &NB Sp Private Random Rnd; INT length = 5; ///<summary> ///Verification code Length (default 6 authenticode length) ///</summary> Pub LIC int Length { get {return Length;} set {length = value; } } int fontsize = 20; ///<summary> ///Verification Code font size (for display distortion effect, default 30 pixel, can modify itself) ///</summary> public int fontsize &N Bsp { get {return fontsize;} set {fontsize = value} } int padding = 4; ///<summary> ///Border repair (default 4 pixels) ///</summary> public int Padding { get {return Padding;} set {Padding = V Alue; } } bool chaos = true; ///<summary> ///output dryness point (default output) ///</summary> Public b Ool Chaos { get {return Chaos;} set {Chaos = Valu E } } Color chaoscolor = Color.lightgray; ///<summary> ///output dryness point color (default gray) </summary> Public Color Chaoscolor { Chaosc Olor; } set {Chaoscolor = value;} } int chaoswight = 1; ///<summary> ///output dryness point concentration ///</summary> public int Ch Aoswight { get {return chaoswight;} set {Chaoswig HT = value; } } Color backgroundcolor = Color.White; ///<summary> ///custom background color (default white) ///</summary> public C Olor backgroundcolor { get {return backgroundcolor;} &NBS P set {backgroundcolor = value;} } color[] colors = {color.black, color.red, Color.dar Kblue, Color.green, Color.orange, Color.brown, Color.darKcyan, color.purple}; ///<summary> ///custom random color array ///</summary> public Colo R[] Colors { get {return Colors;} set {Colors = V Alue; } } string[] fonts = {"Arial", "Georgia"}; ///<summary> ///custom font array ///</summary> public string [] Fonts { get {return Fonts;} set {Fonts = value ; { } #region generating waveform filter effect Private Const double PI = 3.14159265358979323 84626433832795; Private Const double PI2 = 6.283185307179586476925286766559; ///<summary> ///sine wave distorted picture (Edit by 51aspx.com) ///</summary > ///<param name= "srcbmp" > Picture path </PARAM> ///<param name= "Bxdir" > If the Warp is selected true</param> ///<param name= "Nmultvalue" > Waveform amplitude Multiples, the greater the degree of distortion, the general 3</param> ///<param name= "Dphase" > Waveform starting phase, take the value range [0-2*pi) </ param> ///<returns></returns> public System.Drawing.Bitmap twistimage ( Bitmap srcbmp, bool Bxdir, double dmultvalue, double dphase) { System.Drawing.Bi TMap destbmp = new Bitmap (srcbmp.width, srcbmp.height); //fill the bitmap background with white System.Drawing.Graphics graph = System.drawin G.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; &n Bsp DX = Bxdir? (PI2 * (double) j)/Dbaseaxislen: (PI2 * (double) i)/Dbaseaxislen; DX + dphase; Double dy = math.sin (dx); //Get current point's color &NBSP ; 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 &NBSP ; && noldy >= 0 && Noldy < destbmp.height) &nbs P { destbmp. SetPixel (NOLDX, noldy, color); } } return destbmp; } #endregion ///<summary> /// Parity code Pictures ///</summary> ///<param name= "code" > Verification Code </param> ///& lt;returns></returns> Public Bitmap createimage (string code) { & nbsp INT fsize = fontsize; int fwidth = fsize + Padding; int imagewidth = (int) (code. Length * fwidth) + 4 + Padding * 2; int imageheight = fsize * 2 + Padding * 2; SYSTEM.DRAWING.BITMAP image = new System.Drawing.Bitmap (imageWidth-10, ImageHeight- 10); Graphics g = graphics.fromimage (image); g.clear (backgroundcolor); //Add randomly generated dryness point to background for if (this. Chaos) { Pen pen = new Pen (chaoscolor, 0); &n Bsp INT c = chaoswight * 10; for (int i = 0; i < C; i++) { int x = rnd. Next (image. Width); &nbSp int y = rnd. 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 character for (int i = 0; i < code.) Length; i++) { CIndex = rnd. Next (colors.length-1); Findex = rnd. Next (fonts.length-1); &NBSP 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 &NB Sp { top = TOP1, , } & nbsp left = i * fwidth; g.drawstring (code. Substring (i, 1), F, B, left, top); } /Draw a border border color 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; } ///<summary> ///generate random character code ///</summary> &nbs P ///<param name= "Codelen" > String length </param> ///<param name= "Zhcharscount" > Chinese character number </param> ///<returns></returns> public string createverifycode (int Codelen, int zhcharscount) { char[] CHS = new Char[codelen]; &NBSP ; INT index; for (int i = 0; i < Zhcharscount i++) { & nbsp index = rnd. Next (0, Codelen); if (chs[index] = = ") Chs[index] = Createzhchar (); else -I. } for (int i = 0; i < Codelen; i++) { if (chs[i] = = ') CHS [I] = Createenornumchar (); } return new string (CHS, 0, CHS. Length); } ///<summary> ///Generate default length 5 random character code ///</summary> ; ///<returns></returns> public string Createverifycode () { &N Bsp return Createverifycode (Length, 0); } ///<summary> ///generate English or digital characters ///</summary> &N Bsp ///<returns></returns> protected char Createenornumchar () { return Englishorn Umchars[rnd. Next (0, englishornumchars.length)]; } ///<summary> ///generation character ///</summary>   ; ///<returns></returns> protected char Createzhchar () { &NBSP ; //If a Chinese character set is provided, query Chinese character set select Chinese characters if (chinesechars.length > 0) {  ; return CHINESECHARS[RND. Next (0, chinesechars.length)]; } //If no Chinese character set is provided, the Chinese character   is constructed according to the code rules of the GB2312 Simplified Chinese Code table ; else { byte[] bytes = new byte[2]; &NB Sp //First byte value between 0xb0, 0xf7 bytes[0] = (byte) rnd. Next (0xb0, 0xf8); //second byte value in 0xa1, 0xFE between bytes[1] = (byte ) Rnd. Next (0xa1, 0xff); //decoding from encoding byte array Chinese characters String str 1 = encoding.getencoding ("gb2312"). GetString (bytes); return str1[0]; } } }