1. Create an ASP. NET Server Control
2. Create a class in the server control and assign the value as follows:
3. Drag the file directly on the webpage after the file is generated.
4. Call bool B = This. validatenumber1.checksn (this. textbox1.text); then OK.
5. This is just a basic experience for yourself, but it is also a good experience.
-------------------------------- Source code ----------------------------------------
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. drawing;
Using system. Drawing. drawing2d;
Using system. IO;
Namespace validatenumber
{
Public class validatenumber: Control
{
Public void createcode (){
// 1. Define the verification code color, breakfast, and displayed characters.
// Color list for verification code, noise, and noise
Color [] color = {color. Black, color. Red, color. Black, color. Green, color. Orange, color. Brown, color. darkblue };
// Font list for verification code
String [] font = {"Times New Roman", "Ms mincho", "Book antiqua", "gungsuh", "pmingliu", "impact "};
// The Character Set of the Verification Code. Some confusing characters are removed.
Char [] character = {'2', '3', '4', '5', '6', '8', '9', 'A ', 'B', 'C', 'D', 'E', 'F', 'G', 'h', 'J', 'k', 'l ', 'M', 'n', 'P', 'R','s, 't', 'w', 'x', 'y '};
Random RND = new random ();
// 2. The verification code is generated and saved.
String chkcode = "";
// Generate a verification string
For (INT I = 0; I <4; I ++)
{
Chkcode + = character [RND. Next (character. Length)];
}
// Save the verification code in cookie Mode
Httpresponse resp = This. Page. response;
Resp. Cookies ["validatecookie"]. value = chkcode;
Resp. Cookies ["validatecookie"]. expires = datetime. Now. addminutes (2 );
// 3. Draw noise and noise lines
Bitmap BMP = new Bitmap (100, 30); // an image with a length of 100 and a width of 30 PX
Graphics G = graphics. fromimage (BMP );
G. Clear (color. White );
// Draw five noise lines
For (INT I = 0; I <5; I ++)
{
// Randomly generate the start and end points of the noise line
Int X1 = RND. Next (100 );
Int Y1 = RND. Next (30 );
Int X2 = RND. Next (100 );
Int y2 = RND. Next (30 );
// Randomly generate the noise line color
Color CLR = color [RND. Next (color. Length)];
// Draw the noise line
G. drawline (new pen (CLR), X1, Y1, X2, Y2 );
}
// 4. Draw Noise
For (INT I = 0; I <100; I ++)
{
Int x = RND. Next (BMP. width );
Int y = RND. Next (BMP. Height );
Color CLR = color [RND. Next (color. Length)];
BMP. setpixel (X, Y, CLR );
}
// 5, draw the verification string
For (INT I = 0; I <chkcode. length; I ++)
{
String fnt = font [RND. Next (font. Length)];
Font Ft = new font (fnt, 16 );
Color CLR = color [RND. Next (color. Length)];
G. drawstring (chkcode [I]. tostring (), FT, new solidbrush (CLR), (float) I * 20 + 20, (float) 6 );
}
// 6. Write the verification code image to the memory stream and output it in image/PNG format.
Memorystream MS = new memorystream (); // memory stream
Try
{
BMP. Save (MS, system. Drawing. imaging. imageformat. PNG );
Resp. clearcontent ();
Resp. contenttype = "image/PNG ";
Resp. binarywrite (Ms. toarray ());
Resp. Flush ();
Resp. End ();
}
Finally
{
// Release resources
BMP. Dispose ();
G. Dispose ();
}
}
// 7. Verification Method
Public bool checksn (string Sn ){
Return (SN. toupper () = This. Page. Request. Cookies ["validatecookie"]. value. tostring (). toupper ());
}
// 8. Create a verification code
Protected override void oninit (eventargs E)
{
Base. oninit (E );
// Development Environment Design view
If (this. designMode ){
Return;
}
// If it is not a Verification Code request
String STR = This. Page. Request. querystring ["_ imagetag"];
If (STR = "1 "){
This. createcode (); // generate the verification code
}
}
Protected override void render (htmltextwriter writer)
{
If (! This. designMode) {// design view
Writer. write ("} Else {
Writer. Write ("Verification code control ");
}
}
}
}