Not very beautiful, but used to study its principles;
Is to create a new ashx file (general processing program, because I am still learning ......)
Add a call to the form in the ASPX page;
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 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;
}
}
}
}