C # picture verification code

xiaoxiao2021-03-06  75

using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls Using system.web.ui.htmlcontrols; use system.io; Namespace pr.t_img {///

/// createImg's summary description. /// public class createimg: system.Web.ui.page {private void page_load (Object sender, system.eventargs e) {// Place user code here to initialize the page string vnum; vnum = getByrndNum 6); response.clearContent (); // Need to output image information To modify http header Response.contentType = "image / png"; response.binaryWrite (GetByValidatecode (vnum) .toarray ());

} Public MemoryStream GetByValidateCode (string VNum) // Returns memory stream {Bitmap Img = null; Graphics g = null; MemoryStream ms = null; Random random = new Random (); int gheight = VNum.Length * 12; Img = new Bitmap (GHEIGHT, 24); g = graphics.fromage (img); font f = new font ("arial", 12, fontstyle.bold); g.clear (GetByrandColor (204, 255)); // Setting background color Pen Blackpen = New Pen (Color.ivory, 3); for (int i = 0; i <155; i ) // randomly generates the interference line, so that the authentication code in the image is not easily detected by other programs {INT x = Random. NEXT (GHEIGHT); int y = random.next (20); int xl = random.next (6); int yl = random.next (2); g.drawline (Blackpen, x, y, x xl, y YL);} Solidbrush S = New Solidbrush (Color.SandyBrown); g.drawstring (VNUM, F, S, 3, 3); MS = new memoryStream (); img.save (ms, system.drawing.Image. Imageformat.png); g.dispose (); img.dispose (); return ms;} // ----------------- Given range Get random colors ---- -------- Color GetByrandColor (int FC, INT BC) {random random = new random (); if (fc> 255) Fc = 255; if (bc> 255) BC = 255; // i F (AC> 255) AC = 255; int R = fc random.next (bc-fc); int g = fc random.next (bc-fc); int b = fc random.next (BC-BC ); Color = color.fromargb (r, g, b); return r;

// ---------------------- Take the randomly generated authentication code (6 digits) public string getByrndNum (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 ="; int TEMP = -1; // Record last time Random value, try to avoid producing several random number // using a simple algorithm to ensure different random rand = new random (); for (int i = 1; i /// Designer Supports the required method - Do not use the code editor to modify the // / this method. /// private void initializecomponent () {this.load = new system.eventhandler (this.page_load);} #ENDREGON}}

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

New Post(0)