Leave a message if you cannot understand it.
Generate random code class: sigowayrandom. CS
CopyCode The Code is as follows: using system;
Namespace rongyi. model. Common
{
/// <Summary>
/// Summary of sigowayrandom
/// </Summary>
Public class sigowayrandom
{
# Region get the verification code
/// <Summary>
/// Obtain the verification code
/// </Summary>
/// <Returns> check code character array </returns>
Public static string [] getcheckcode ()
{
String [] strcheckcode = new string [4];
// The system time in milliseconds is a Random Seed
Int nseed = convert. toint16 (datetime. Now. millisecond );
Random random = new random (nseed );
// Generate a random number of 0-9
Strcheckcode [0] = convert. tostring (random. Next (1, 10 ));
// Generate random letters of A-Z and A-Z
Strcheckcode [1] = sigowayrandom. getletter (random );
Strcheckcode [2] = convert. tostring (random. Next (1, 10 ));
Strcheckcode [3] = sigowayrandom. getletter (random );
// Return the verification code
Return strcheckcode;
}
# Endregion
# Region get letters, case sensitive
/// <Summary>
/// Obtain letters, case sensitive
/// </Summary>
/// <Returns> uppercase and lowercase letters </returns>
Private Static string getletter (random)
{
// Random Number
Int nchar = random. Next (65,122 );
// Non-ASCII
If (nchar> = 91 & nchar <= 96)
{
Nchar-= 6;
}
Return convert. tostring (char) nchar );
}
# Endregion
}
}
Draw Verification Code: Sigowaydraw. CSCopy codeThe Code is as follows: using system. drawing;
Using system. Drawing. imaging;
Using system. Web;
Namespace rongyi. model. Common
{
/// <Summary>
/// Summary of sigowaydraw
/// </Summary>
Public class sigowaydraw
{
# Region Constructor
/// <Summary>
/// Constructor
/// </Summary>
Public sigowaydraw (){}
# Endregion
# Region draw Verification Code
/// <Summary>
/// Draw the verification code
/// </Summary>
/// <Returns> Verification Code </returns>
Public String drawstring ()
{
// Set the font
Font drawfont = new font ("Arial", 10 );
// Create a bitmap Element
Bitmap objbitmap = new Bitmap (50, 20 );
// Create a Drawing Object
Graphics objgraphics = graphics. fromimage (objbitmap );
// Set the canvas background color
Objgraphics. Clear (color. White );
// Obtain a random string
String [] strdrawstring = sigowayrandom. getcheckcode ();
// Draw a string
Objgraphics. drawstring (strdrawstring [0], drawfont, new solidbrush (color. Purple), 1, 2 );
Objgraphics. drawstring (strdrawstring [1], drawfont, new solidbrush (color. Green), 12, 2 );
Objgraphics. drawstring (strdrawstring [2], drawfont, new solidbrush (color. Red), 24, 2 );
Objgraphics. drawstring (strdrawstring [3], drawfont, new solidbrush (color. steelblue), 35, 2 );
// Draw interference lines
Objgraphics. drawline (pens. Silver, 5, 10, 40, 3 );
Objgraphics. drawline (pens. Gray, 10, 5, 45, 15 );
Objgraphics. drawline (pens. hotpink, 15, 20, 30, 10 );
Objgraphics. drawline (pens. lightpink, 10, 15, 35, 20 );
// Draw an image to a bitmap object
Objgraphics. drawimage (objbitmap, 0, 0 );
// Set the path and name of the saved Image
String strfile = httpruntime. appdomainapppath. tostring () + "/resource/img/checkcode.gif ";
// Output file
Objbitmap. Save (strfile, imageformat. GIF );
// Connect the verification code string
String strcheckcode = string. empty;
Foreach (string strtemp in strdrawstring)
{
Strcheckcode + = strtemp;
}
// Return the verification code
Return strcheckcode;
}
# Endregion
}
}