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