This article provides a asp.net to generate validation pictures of the class, the function is to display a simple string, we refer to the use of the bar
Code: Code as follows: public void ProcessRequest (HttpContext context) {context. Response.ContentType = "Image/jpeg"; Creates a bitmap, and gives the specified border a wide-high using (Image img=new Bitmap (80,25)) { ///Create painter object, draw string using in IMG object (Graphics g=graphics.fromimage ( IMG)) { //Set the background color of the bitmap, the default is black G.clear (color.white);//Set the authentication code wide, IMG. Width-1, IMG. Height-1 is primarily a background color that covers the border line G.drawrectangle (pens.black, 0, 0, IMG. Width-1, IMG. HEIGHT-1); Pass 100 noise point, pass painter object, Bitmap object Drawpoint (M, G, IMG); Draw 4 Captcha String Vcode=getcode (4);//vcode here can be assigned to cookies g.drawstring (Vcode, New Font ("Arial", 14, Fontstyle.strikeout | Fontstyle.strikeout),///FontStyle font style, multiple styles, need | line BRUSHES.BLACK, new RectangleF (R.next), R.next (7), Img. Width, IMG. Height)); Img. Save (context. Response.outputstream, System.Drawing.Imaging.ImageFormat.Jpeg)//Save Authenticode object, specify Jpeg format }} //Draw Noise method void Drawpoint (int point,graphics g,image img) {for (int i = 0; i < point; i++) {int x = R.next (img. Width); int y = R.next (img. Width); G.dRawline (pens.red, New Point (X, y), New Point (X+2, y+2)); } //random number Random r = new Random (); //Reprint string GetCode (int point) {string txtstr = "asf2345we5r9f3hmbcz455k";//The string string here will be converted to a char array. Arabic numerals 1 and lowercase l better not be written in it. char[] Chararr = Txtstr.toarray (); int num = 0; String code = ""; for (int i = 0; I <point; i++) {num = R.next (chararr.length); code +=chararr[num];} return code; }