asp.net create bitmap build Verify Picture Class (Verification code Class) _ Practical Tips

Source: Internet
Author: User

Code:

Copy Code code as follows:

public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Image/jpeg";
Creates a bitmap and gives the specified border a wide height
using (Image img=new Bitmap (80,25))
{

Create painter object, draw string in IMG Object
using (Graphics g=graphics.fromimage (IMG))
{
Sets the background color of the bitmap, which is black by default
G.clear (Color.White);
Set the width of the verification code, IMG. Width-1, IMG. Height-1 is mainly background color covered with border line
G.drawrectangle (pens.black, 0, 0, img.) Width-1, IMG. HEIGHT-1);
Pass 100 noises, painter objects, bitmap objects
Drawpoint (M, G, IMG);
Draw a string of 4 verification codes
String Vcode=getcode (4);//vcode can be assigned to cookies here

g.DrawString (Vcode,
New Font ("Arial", 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

}
}
}

How to draw the noise point

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 Chong
String GetCode (int point)
{
String txtstr = "asf2345we5r9f3hmbcz455k";//The string string here will be converted to a char array, and Arabic numerals 1 and lowercase l should not be written in.
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;
}

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.