Asp tutorial. net verification code control
Using system;
Using system. collections. generic;
Using system. linq;
Using system. web;
Using system. drawing;
Using system. drawing. imaging;
Using system. web. sessionstate;
Namespace webapp
{
/// <Summary>
/// Summary of the verification code instance
/// </Summary>
Public class verification code instance: ihttphandler, irequiressessionstate // this interface must be implemented when session is used in a general processing program, in system. web. sessionstate;
{
Public void processrequest (httpcontext context)
{
Context. response. contenttype = "image/jpeg"; // The jpg type is returned;
Using (bitmap = new bitmap (140, 80) // pixel size;
{
Using (graphics g = graphics. fromimage (bitmap) // Generate a canvas
{
Random rand = new random ();
Int code = rand. next (1000,999 999); // specify a random function to limit the random string size;
String strcode = code. tostring ();
Httpcontext. current. session ["code"] = strcode; // use the session interface in a general handler;
G. clear (color. yellowgreen); // specifies the background color;
G. drawstring (strcode, new font ("", 20), brushes. white, new pointf (15, 25); // image parameters, (font, size), color, and position;
Bitmap. save (context. response. outputstream, imageformat.jpeg); // output to the stream and save it as jpg;
}
}
}
Public bool isreusable
{
Get
{
Return false;
}
}
}
}