A simple ASP. NET verification code and a simple ASP. NET Verification Code
The example in this article shares the code of the ASP. NET verification code for your reference. The details are as follows:
I mainly see the interference line. If there is no interference line in a verification code, at least the noise and random code layout should work:
/// <Summary> /// verification code generation class /// </summary> public class verify_code: IHttpHandler, IRequiresSessionState {public void ProcessRequest (HttpContext context) {int codeW = 80; int codeH = 22; int fontSize = 16; string chkCode = string. empty; // Color list for verification code, noise line, and noise color [] Color = {Color. black, Color. red, Color. blue, Color. green, Color. orange, Color. brown, Color. brown, Color. darkBlue}; // font list for verification code string [] font = {"Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact "}; // The character Set of the Verification Code. Some confusing characters char [] character = {'0', '1', '2', '3', '4' are removed ', '5', '6', '8', '9'}; Random rnd = new Random (); // generate the verification code string for (int I = 0; I <4; I ++) {chkCode + = character [rnd. next (character. length)];} // write the Session context. session ["sys_verify_code"] = chkCode; // create the canvas Bitmap bmp = new Bitmap (codeW, codeH); Graphics g = Graphics. fromImage (bmp); g. clear (Color. white); // draw the noise line for (int I = 0; I <4; I ++) {int x1 = rnd. next (codeW); int y1 = rnd. next (codeH); int x2 = rnd. next (codeW); int y2 = rnd. next (codeH); Color clr = color [rnd. next (color. length)]; g. drawLine (new Pen (clr), x1, y1, x2, y2);} // draw the verification code string for (int I = 0; I <chkCode. length; I ++) {string fnt = font [rnd. next (font. length)]; Font ft = new Font (fnt, fontSize); Color clr = color [rnd. next (color. length)]; g. drawString (chkCode [I]. toString (), ft, new SolidBrush (clr), (float) I * 18 + 2, (float) 0);} // draw noise for (int I = 0; I <100; I ++) {int x = rnd. next (bmp. width); int y = rnd. next (bmp. height); Color clr = color [rnd. next (color. length)]; bmp. setPixel (x, y, clr);} // clears the output cache of the page and sets that the page has no cache context. response. buffer = true; context. response. expiresAbsolute = System. dateTime. now. addMilliseconds (0); context. response. expires = 0; context. response. cacheControl = "no-cache"; context. response. appendHeader ("Pragma", "No-Cache"); // write the verification code image to the memory stream, and output MemoryStream MS = new MemoryStream (); try {bmp. save (MS, ImageFormat. png); context. response. clearContent (); context. response. contentType = "image/Png"; context. response. binaryWrite (ms. toArray ();} finally {// explicitly releases the resource bmp. dispose (); g. dispose () ;}} public bool IsReusable {get {return false ;}}}
Basic verification code generation demo:
Using System; using System. drawing; using System. drawing. imaging; using System. IO; using System. web; public partial class image: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {string tmp = RndNum (4); HttpCookie a = new HttpCookie ("ImageV", tmp); Response. cookies. add (a); this. validateCode (tmp);} private void ValidateCode (string VNum) {Bitmap Img = null; Graphics g = null; M EmoryStream MS = null; int gheight = VNum. length * 12; Img = new Bitmap (gheight, 25); g = Graphics. fromImage (Img); // background color g. clear (Color. white); // text Font f = new Font ("Arial Black", 10); // text Color: SolidBrush s = new SolidBrush (Color. black); g. drawString (VNum, f, s, 3, 3); MS = new MemoryStream (); Img. save (MS, ImageFormat. jpeg); Response. clearContent (); Response. contentType = "image/Jpeg"; Response. BinaryWrite (ms. toArray (); g. dispose (); Img. dispose (); Response. end ();} private string RndNum (int VcodeNum) {string Vchar = ", a, B, c, d, e, f, g, h, I, j, k, l, m, n, o, p "+", q, r, s, t, u, v, w, x, y, z "; string [] VcArray = Vchar. split (new Char [] {','}); string VNum = ""; int temp =-1; Random rand = new Random (); for (int I = 1; I <VcodeNum + 1; I ++) {if (temp! =-1) {rand = new Random (I * temp * unchecked (int) DateTime. now. ticks);} int t = rand. next (35); if (temp! =-1 & temp = t) {return RndNum (VcodeNum);} temp = t; VNum + = VcArray [t];} return VNum ;}}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.