ASP.NET (C #) implementation verification code function
Create a new page of the page ValidateCode.aspx for creating a verification code picture, its background CS file code is as follows: PageLoad
Private
Void
Page_load
Object
Sender, System.EventArgs E)
{String Checkcode = CreaterandMcode (4); session ["Checkcode"] = Checkcode; Creckcode (checkcode);
Where CreaterandMcode is a custom function, the parameter represents the number of verification code bits.
Private
String
CreaterAndomcode
int
CodeCount
{String Allchar = "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, Z "; string [] allchararray = allchar.split (','); string randomcode =" "; int temp = 1; random rand = new random (); for (int i = 0; i CREATEIMAGE is also a custom function for generating diagrams Private Void CREATEIMAGE String Checkcode {Int iwidth = (int) (checkCode.Length * 11.5); System.Drawing.Bitmap image = new System.Drawing.Bitmap (iwidth, 20); Graphics g = Graphics.FromImage (image); Font f = new System. Drawing.font ("Arial", 10, System.drawing.FontStyle.bold; brush b = new system.drawing.solidbrush (color.White); //g.FillRectangle (New System.drawing.Solidbrush (Color.Blue ), 0, 0, image.width, image.Height); g.clear (color.blue); g.drawstring (Checkcode, F, B, 3, 3); Pen Blackpen = New Pen (Color.Black, 0 ); Random rand = new random (); for (int i = 0; i <5; i ) {int y = rand.next (image.height); g.drawline (Blackpen, 0, Y, Image.Width, y);} system.io.MemoryStream ms = new system.io.MemoryStream (); image.save (ms, system.drawing.Image.imageformat.jpeg); response.clearcontent (); response.contenttype = "iMA GE / JPEG "; response.binaryWrite (ms.toarray ()); g.dispose (); image.dispose ();} // g.fillRectangle (new system.drawing.solidbrush (color.blue), 0,0 , image.width, image.Height; g.clear (color.blue); Both methods can change the background color of the generated picture below the FOR cycle to generate some random horizontal lines in the page that needs to be used to verify code Add a < ASP: Image Runat = " Server " Id = " Imagecheck " ImageURL = "Validatecode.aspx" > ASP: Image > Posted on 2004-10-20 17:03 Side City Langon Read (2313) Comments (34) Editor's ----------------------------------- Create a new page ValidateCode.aspx that is specifically used to create a verification code picture, its background CS file code is as follows: PageLoad private void Page_Load object sender, System.EventArgs e) {string checkCode = CreateRandomCode 4); Session [ "CheckCode"] = checkCode; CreateImage checkCode);} where CreateRandomCode is defined functions representing the parameters private string digit codes Createrandomcode int codecount) {string allchar = "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, Y, Z "; string [] allchararray = allchar.split ','); string randomcode =" "; int Temp = -1; random rand = new random); for int i = 0; i private void CreateImage string checkCode) {int iwidth = int) checkCode.Length * 11.5); System.Drawing.Bitmap image = new System.Drawing.Bitmap iwidth, 20); Graphics g = Graphics.FromImage image); Font f = new System.Drawing.Font "Arial", 10, System.Drawing.FontStyle.Bold); Brush b = new System.Drawing.SolidBrush Color.White); //g.FillRectangle new System.Drawing.SolidBrush Color.Blue), 0,0, image.width, image.height); g.drawstring Checkcode, F, B, 3, 3); Pen Blackpen = New Pen Color.Black, 0); Random Rand = New random); for int i = 0; i <5; i ) {int y = rand.next image.Height); g.drawline Blackpen, 0, y, image.width, y);} system.io.MemoryStream ms = new System.IO.MemoryStream); image.Save ms, System.Drawing.Imaging.ImageFormat.Jpeg); Response.ClearContent); Response.ContentType = "image / Jpeg"; Response.BinaryWrite ms.ToArray)); g.dispose); image.dispose);} //g.FillRectangle new system.drawing.solidbrush color.blue), 0,0, image.width, image.height); g.clear color.bl UE); Both methods can change the background color of the generated picture below the FOR cycle to generate some random horizontal lines Add a PageLoad private void Page_Load object sender, System.EventArgs e) {string checkCode = CreateRandomCode 4); Session [ "CheckCode"] = checkCode; CreateImage checkCode);} where CreateRandomCode is defined functions representing the parameters private string digit codes Createrandomcode int codecount) {string allchar = "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, Y, Z "; string [] allchararray = allchar.split ','); string randomcode =" "; int Temp = -1; random rand = new random); for int i = 0; i private void CreateImage string checkCode) {int iwidth = int) checkCode.Length * 11.5); System.Drawing.Bitmap image = new System.Drawing.Bitmap iwidth, 20); Graphics g = Graphics.FromImage image); Font f = new System.Drawing.Font "Arial", 10, System.Drawing.FontStyle.Bold); Brush b = new System.Drawing.SolidBrush Color.White); //g.FillRectangle new System.Drawing.SolidBrush Color.Blue), 0,0, image.width, image.height); g.drawstring Checkcode, F, B, 3, 3); Pen Blackpen = New Pen Color.Black, 0); Random Rand = New random); for int i = 0; i <5; i ) {int y = rand.next image.Height); g.drawline Blackpen, 0, y, image.width, y);} system.io.MemoryStream ms = new System.IO.MemoryStream); image.Save ms, System.Drawing.Imaging.ImageFormat.Jpeg); Response.ClearContent); Response.ContentType = "image / Jpeg"; Response.BinaryWrite ms.ToArray)); g.dispose); image.dispose);} //g.FillRectangle new system.drawing.solidbrush color.blue), 0,0, image.width, image.height); g.clear color.bl UE); Both methods can change the background color of the generated picture below the FOR cycle to generate some random horizontal lines Add a PageLoad private void Page_Load object sender, System.EventArgs e) {string checkCode = CreateRandomCode 4); Session [ "CheckCode"] = checkCode; CreateImage checkCode);} where CreateRandomCode is defined functions representing the parameters private string digit codes Createrandomcode int codecount) {string allchar = "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, Y, Z "; string [] allchararray = allchar.split ','); string randomcode =" "; int Temp = -1; random rand = new random); for int i = 0; i private void CreateImage string checkCode) {int iwidth = int) checkCode.Length * 11.5); System.Drawing.Bitmap image = new System.Drawing.Bitmap iwidth, 20); Graphics g = Graphics.FromImage image); Font f = new System.Drawing.Font "Arial", 10, System.Drawing.FontStyle.Bold); Brush b = new System.Drawing.SolidBrush Color.White); //g.FillRectangle new System.Drawing.SolidBrush Color.Blue), 0,0, image.width, image.height); g.drawstring Checkcode, F, B, 3, 3); Pen Blackpen = New Pen Color.Black, 0); Random Rand = New random); for int i = 0; i <5; i ) {int y = rand.next image.Height); g.drawline Blackpen, 0, y, image.width, y);} system.io.MemoryStream ms = new System.IO.MemoryStream); image.Save ms, System.Drawing.Imaging.ImageFormat.Jpeg); Response.ClearContent); Response.ContentType = "image / Jpeg"; Response.BinaryWrite ms.ToArray)); g.dispose); image.dispose);} //g.FillRectangle new system.drawing.solidbrush color.blue), 0,0, image.width, image.height); g.clear color.bl UE); Both methods can change the background color of the generated picture below the FOR cycle to generate some random horizontal lines Add a In order to learn from everyone, modify VB.NET Code to become a C # gif.aspx file modified content private void page_load (Object sender, system.eventargs e) {// Place the user code here to initialize page // rNDNUM is a customize function string vNum = RndNum (5); Session [ "vNum"] = vNum; ValidateCode (vNum);} private void ValidateCode (string vNum) {// generates an authentication code System.Drawing.Bitmap img; System.Drawing.Graphics g System.IO.MemoryStream ms; INT GHEIGHT = Convert.Toint32 (vnum.Length * 11.5); // Gheight For the image width, automatically change image width IMG = New Bitmap (G HT, 20) according to character lengths; g = graphics. FromImage (IMG); // Draw strings in the rectangular shape (string, font, brush color, upper left x. Y) G. DrawString (VNUM, New Font ("Arial", 10), New Solidbrush (SYSTEM.DRAWING .COLOR.BLUE), 3, 3); MS = new memorystream (); img.save (ms, imageformat.png); response.clearcontent (); // Need to output image information To modify http head Response.contentType = "image / gif"; response.binarywrite (ms.toarray ()); g.dispose (); img.dispose (); response.end ();} // '' function name: rndnum // 'function parameters : Vcodenum - Set the number of bits // 'function functions for returning random strings: random string of numbers and characters mixed private 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 (", ". Tochararray ()); // Generate a string string vnum =" INT i; for (i = 1; i