Here I have written the Asp.net verification code, which can be used directly in my own project. This avoids re-writing the code next time, that is, what we often say is "tailism". I will always learn something, here we will also explain the important knowledge points used in the application: 1. Pay attention to the reference of using system in the namespace. drawing;
Using system. Drawing. drawing2d;
Using system. drawing. imaging; these empty namespaces are directly related to the drawing we wrote. Class 2: bitmap: class used to generate image graphics: drawing class, to apply the drawline () method to the generated image and its drawstring () method () method To write the string. However, before writing the string, you must declare the image brush to write the string: declare the image brush system. drawing. drawing2d. lineargradientbrush brush; its drawrectangle () method draws a giant border, and finally stores the SAVE () method for generating Bitmap bitmap. If you need to draw a vertex, it is also implemented by using the setpixel () method of Bitmap class. The following are the classes that have been written to generate verification codes. You can use them directly to facilitate my future work and study.
Using system;
Using system. Web;
Using system. Web. Security;
Using system. drawing;
Using system. Drawing. drawing2d;
Using system. Drawing. imaging;
Public partial class validate_img: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Createcheckcodeimage (generatecheckcode ());
}
/// <Summary>
/// Generate a string
/// </Summary>
/// <Returns> </returns>
Private string generatecheckcode ()
{
Int number;
Char code; // character code
String checkcode = string. Empty, sendcoder;
System. Random random = new random ();
For (INT I = 0; I <5; I ++)
{
Number = random. Next ();
If (Number % 2 = 0)
Code = (char) ('0' + (char) (Number % 10 ));
Else
{
Code = (char) ('A' + (char) (Number % 26 ));
}
Checkcode + = code. tostring ();
}
Sendcoder = checkcode. tolower ();
// Put the string in the cookie
Response. Cookies. Add (New httpcookie ("checkcode", sendcoder ));
Return checkcode;
}
/// <Summary>
/// Generate an image
/// </Summary>
/// <Param name = "checkcode"> </param>
Public void createcheckcodeimage (string checkcode)
{
If (checkcode = NULL | checkcode. Trim () = string. Empty)
Return;
System. Drawing. bitmap image = new system. Drawing. Bitmap (INT) math. Ceiling (checkcode. length * 13.5), 25); // The height is 25.
Graphics G = graphics. fromimage (image );
Try
{
// Generate a random Generator
Random random = new random ();
// Clear the background color of the image
G. Clear (color. White );
// Draw the background noise line of the image
For (INT I = 0; I <25; I ++)
{
Int X1 = random. Next (image. width );
Int X2 = random. Next (image. width );
Int Y1 = random. Next (image. Height );
Int y2 = random. Next (image. Height );
G. drawline (new pen (color. greenyellow), X1, Y1, X2, Y2 );
}
// Write characters
Font font = new system. Drawing. Font ("verdana", 12, (system. Drawing. fontstyle. Bold | system. Drawing. fontstyle. italic ));
// Write with a paint brush <use a pen to draw and write with a paint brush>
System. drawing. drawing2d. lineargradientbrush brush = new system. drawing. drawing2d. lineargradientbrush (New rectangle (0, 0, image. width, image. height), color. blue, color. darkred, 1.2f, true );
G. drawstring (checkcode, Font, brush, 2, 2 );
// Foreground noise of the image
For (INT I = 0; I <80; I ++)
{
Int x = random. Next (image. width );
Int y = random. Next (image. Height );
Image. setpixel (X, Y, color. fromargb (random. Next ()));
}
// Draw the border line of the image
G. drawrectangle (new pen (color. Red), 0, 0, image. Width-1, image. Height-1 );
System. Io. memorystream MS = new system. Io. memorystream ();
Image. Save (MS, system. Drawing. imaging. imageformat. GIF );
Httpcontext. Current. response. clearcontent ();
Httpcontext. Current. response. contenttype = "image/GIF ";
Httpcontext. Current. response. binarywrite (Ms. toarray ());
}
Finally
{
G. Dispose ();
Image. Dispose ();
}
}
}