Verification codes are always used for website login. Generating verification codes is very easy for C. The specific code is as follows:
1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. web; 5 using system. web. ui; 6 using system. web. UI. webcontrols; 7 using system. drawing; 8 9 namespace Ajax. verification Code 10 {11 public partial class Yanma: system. web. UI. page12 {13 private int codenum = 4; 14 private string codenum = ""; 15 protected void page_load (Object sender, eventargs e) 16 {17 veriflcationcode (); 18 response code (codenum); 19} 20 21 private void veriflcationcode () 22 {23 // generate Verification Code 24 string allchar = ", 0,, b, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, w, X, Y, Z "; 25 string [] strchar = allchar. split (','); 26 random RD = new random (); 27 string Codes = ""; 28 for (INT I = 0; I <codenum; I ++) 29 {30 codes + = strchar [Rd. next (0, 35)]; 31} 32 codenum = codes; 33 session ["vercode"] = codes; 34} 35 36 private void encode code (string codes) 37 {38 int iwidth = (INT) (codes. length * 17); // defines the width of the canvas 39 system. drawing. bitmap images = new system. drawing. bitmap (iwidth, 25); // declare a layout class, initialize the width and height of the image 40 41 graphics G = graphics. fromimage (images); 42 font F = new font ("Arial", 14); // defines the text type 43 brush B = new system. drawing. solidbrush (color. black); // text color: Black 44 brush r = new system. drawing. solidbrush (color. fromargb (166, 8, 8); // The text color is 45 GB. clear (system. drawing. colortranslator. fromhtml ("# 99c1cb"); // background color 46 47 char [] CH = codes. tochararray (); 48 for (INT I = 0; I <ch. length; I ++) 49 {50 if (CH [I]> = '0' & Ch [I] <= '9 ') 51 {52 // The number is displayed in red 53g. drawstring (CH [I]. tostring (), F, R, 3 + (I * 14), 3 ); // draw text, font type, font color, font spacing, font spacing 54} 55 else56 {// The letter is displayed in black 57 GB. drawstring (CH [I]. tostring (), F, B, 3 + (I * 14), 3 ); 58} 59} 60 61 // For loop to generate some random horizontal lines 62 pen blackpen = new pen (color. black, 0); 63 random Rand = new random (); 64 for (INT I = 0; I <5; I ++) 65 {66 int y = Rand. next (images. height); 67g. drawline (blackpen, 0, Y, images. width, Y); 68} 69 70 71 system. io. memorystream MS = new system. io. memorystream (); 72 images. save (MS, system. drawing. imaging. imageformat. JPEG); 73 // history back does not repeat 74 response. cache. setnostore (); // This sentence is 75 response. clearcontent (); 76 response. contenttype = "image/JPEG"; 77 response. binarywrite (Ms. toarray (); 78g. dispose (); 79 images. dispose (); 80}
Effect: