Generate picture verification code test

xiaoxiao2021-03-05  31

Share a Simple Way to Generate A Code for Validation Base on Image.

In image.aspx.cs:

Using system.drawing; using system.drawing.Imaging; private void page_load (object sender, system.eventargs e) {drawimg ();}

private void drawimg () {Bitmap newBitmap = new Bitmap (36,16, PixelFormat.Format32bppArgb); // draw image Graphics g = Graphics.FromImage (newBitmap); // fill the area with color g.FillRectangle (new SolidBrush (Color . White), New Rectangle (0, 0, 36, 16)); // SET FONT OBJECT FONT TEXTFONT = New Font ("Verdana", 9); // Create Rectanglef Structure Area Rectanglef Rectangle = New Rectanglef (0,0 , 36, 16); // set a random object random rd = new random (); // get random number int valutionNO = 1000 rd.next (8999); session ["valation"] = valationno; // Fill with color g.FillRectangle (new SolidBrush (Color.BurlyWood), rectangle); // fill with fonts g.DrawString (valationNo.ToString (), textFont, new SolidBrush (Color.Blue), rectangle); // save the image to memory stream try {System.IO.MemoryStream ms = new System.IO.MemoryStream (); newBitmap.Save (ms, System.Drawing.Imaging.ImageFormat.Jpeg); Response.ClearContent (); Response.ContentType = "image / JPEG "; response.bin Arywrite (Ms.Toarray ());

//newbitmap.save (@"c:/inetpub/wwft/test0401/vaimg.gif" ,imageformat.gif); save it to harddisk} catch (exception ex) {response.write (ex. amount);}

}

Validate-page.aspx ... ...

private void drawimg () {Bitmap newBitmap = new Bitmap (36,16, PixelFormat.Format32bppArgb); // draw image Graphics g = Graphics.FromImage (newBitmap); // fill the area with color g.FillRectangle (new SolidBrush (Color . White), New Rectangle (0, 0, 36, 16)); // SET FONT OBJECT FONT TEXTFONT = New Font ("Verdana", 9); // Create Rectanglef Structure Area Rectanglef Rectangle = New Rectanglef (0,0 , 36, 16); // set a random object random rd = new random (); // get random number int valutionNO = 1000 rd.next (8999); session ["valation"] = valationno; // Fill with color g.FillRectangle (new SolidBrush (Color.BurlyWood), rectangle); // fill with fonts g.DrawString (valationNo.ToString (), textFont, new SolidBrush (Color.Blue), rectangle); // save the image to memory stream try {System.IO.MemoryStream ms = new System.IO.MemoryStream (); newBitmap.Save (ms, System.Drawing.Imaging.ImageFormat.Jpeg); Response.ClearContent (); Response.ContentType = "image / JPEG "; response.bin Arywrite (Ms.Toarray ()); // newbitmap.save (@ "c: /inetpub/wwwroot/test0401/vaimg.gif", Imageformat.gif); Save it to harddisk} catch (exception ex) {response.write (ex.Message);

}

Validate-page.aspx ... ...

Validate-Page.cs

Private boolean check ()

{

IF (session ["PCNUM"]! = null)

{

IF (txtnumber.text.trim () == session ["valation"]. TOSTRING (). Trim ())

{

Return True;

}

Else

{

Return False;

}

}

Else

{

Return False;

}

}

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

New Post(0)