Verification code C # version

zhaozj2021-02-16  66

Private Void Page_Load (Object Sender, System.EventArgs E)

{

/ / Place the user code here to initialize the page

// rNDNUM is a custom function

String vnum = rNDNUM (4); // The number 4 represents the 4-bit verification string!

Session ["VNUM"] = VNUM;

Validatecode (VNUM);

}

// generates an image function private void ValidateCode (string VNum) {int Gheight = (int) (VNum.Length * 7); // gheight of image width, image width automatic change of character length System.Drawing.Bitmap Img = new System .Drawing.bitmap (GHEIGHT, 12); Graphics g = graphics.fromimage (img); g.drawstring (vnum, new system.drawing.font ("Song", 9), New System.drawing.Solidbrush (Color.red ), 0, 0); // Draw strings in the rectangular shape (string, font, brush color, on the left X.- Y) system.io.memorystream ms = new system.io.MemoryStream (); img.save MS, system.drawing.imaging.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

转载请注明原文地址:https://www.9cbs.com/read-16917.html

New Post(0)