asp.net create a bitmap build validation picture class

Source: Internet
Author: User

  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; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.