// Source code for generating the verification code
Using system;
Using system. Data;
Using system. drawing;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public partial class validatecode: system. Web. UI. Page
...{
Protected void page_load (Object sender, eventargs E)
...{
If (! Ispostback)
...{
String checkcode;
If (session [Firefly. BBs. Common. session. checkcode] = NULL)
...{
Checkcode = createrandomcode (4 );
}
Else
...{
Checkcode = session [Firefly. BBs. Common. session. checkcode]. tostring ();
}
Session [Firefly. BBs. Common. session. checkcode] = checkcode;
Createimage (checkcode );
}
}
Private string createrandomcode (INT codecount)
...{
String allchar = ", A, B, C, D, E, F, G, H, I, J, K, L, M, n, O, P, Q, R, S, T, U, w, x, y, z ";
String [] allchararray = allchar. Split (',');
String randomcode = "";
Int temp =-1;
Random Rand = new random ();
For (INT I = 0; I <codecount; I ++)
...{
If (temp! =-1)
...{
Rand = new random (I * temp * (INT) datetime. Now. ticks ));
}
Int T = Rand. Next (35 );
If (temp = T)
...{
Return createrandomcode (codecount );
}
Temp = T;
Randomcode + = allchararray [T];
}
Return randomcode;
}
Private void createimage (string checkcode)
...{
If (checkcode = NULL | checkcode. Trim () = string. Empty)
Return;
System. Drawing. bitmap image = new system. Drawing. Bitmap (checkcode. length * 12 + 10, 22 );
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. Silver), X1, Y1, X2, Y2 );
}
Font font = new system. Drawing. Font ("Arial", 12, (system. Drawing. fontstyle. Bold | system. Drawing. fontstyle. italic ));
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 <100; 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. Silver), 0, 0, image. Width-1, image. Height-1 );
System. Io. memorystream MS = new system. Io. memorystream ();
Image. Save (MS, system. Drawing. imaging. imageformat. GIF );
Response. clearcontent ();
Response. contenttype = "image/GIF ";
Response. binarywrite (Ms. toarray ());
}
Finally
...{
G. Dispose ();
Image. Dispose ();
}
}
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/yumanqing/archive/2007/03/27/1542293.aspx