Refer to one of the others: ASP.NET implementation verification code --------------------------------- Start --- ----------------------------------------------------------------------------------------------------------------------------------------- .Drawing.Imaging; private void page_load (Object sender, system.eventargs e) {// Place user code here to initialize page // rNDNUM is a custom function string vnum = rNDNUM (4); // Here the number 4 Representatives displayed 4-digit verification strings! Session ["vnum"] = vnum; validatecode (vnum);} // Generate image function private void value "{int Gheight = (int)); // Gheight is a picture width, according to Character length automatically change image width system.drawing.bitmap img = new system.drawing.bitmap (GHEIGHT, 20); graphics g = graphics.fromImage (img); g.drawstring (vnum, new system.drawing.font ("Arial ", 10), new system.drawing.Solidbrush (Color.RED), 3, 3); // Draw strings in the rectangular shape (string, font, brush color, top left x. Y) system.io.MemoryStream MS = new system.io.MemoryStream (); img.save (ms, system.drawing.image.imageformat.png); response.clearcontent (); // Need to output image information To modify http head Response.contentType = " Image / png "; response.binarywrite (ms.toarray ()); g.dispose (); img.dispose (); response.end ();}
/ / Generate a random number function randomly extracted from a VCHAR array // letter Case Size Case Case Case Case Case Case Case Case Public 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, W, X, Y, Z "; String [] vcarray = vchar.split (','); string vnum =" "; // Due to the short query, it is not necessary to use the StringBuilder Int Temp = -1; // Record the last random value, try to avoid Production of several random numbers
// With a simple algorithm to ensure different random rand = new random (); for (int i = 1; i Below is Login.aspx in the response event of the Submit button, Public Void Doit (ISVALID) {string vnum; vnum = session ["VNUM"]. TOSTRING (); ViewState ["VNUM"] = vnum; if (this.vcode.text == viewstate ["vnum"]. TOSTRING ()) {