/// <Summary>
/// Rotating visual verification code Image
/// </Summary>
Public class vlidationcode
{
Public Enum randomstringmode
{
/// <Summary>
/// Lowercase letters
/// </Summary>
Lowerletter,
/// <Summary>
/// Uppercase letters
/// </Summary>
Upperletter,
/// <Summary>
/// Uppercase/lowercase letters
/// </Summary>
Letter,
/// <Summary>
/// Number
/// </Summary>
Digital,
/// <Summary>
/// Mixed numbers and uppercase/lowercase letters
/// </Summary>
Mix
}
Public static string generaterandomstring (INT length, randomstringmode Mode)
{
String rndstr = string. empty;
If (length = 0)
Return rndstr;
// Candidate characters are used as arrays to remove unwanted characters, such as numbers 0 and letters o
Char [] digitals = new char [10] {'0', '1', '2', '3', '4', '5', '6 ', '7', '8', '9 '};
Char [] lowerletters = new char [26] {
'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G ',
'H', 'I', 'J', 'k', 'l', 'M', 'n ',
'O', 'P', 'Q', 'R', 's','t ',
'U', 'V', 'w', 'x', 'y', 'z '};
Char [] upperletters = new char [26] {
'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G ',
'H', 'I', 'J', 'k', 'l', 'M', 'n ',
'O', 'P', 'Q', 'R', 's','t ',
'U', 'V', 'w', 'x', 'y', 'z '};
Char [] letters = new char [52] {
'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G ',
'H', 'I', 'J', 'k', 'l', 'M', 'n ',
'O', 'P', 'Q', 'R', 's','t ',
'U', 'V', 'w', 'x', 'y', 'z ',
'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G ',
'H', 'I', 'J', 'k', 'l', 'M', 'n ',
'O', 'P', 'Q', 'R', 's','t ',
'U', 'V', 'w', 'x', 'y', 'z '};
Char [] mix = new char [62] {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9 ',
'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G ',
'H', 'I', 'J', 'k', 'l', 'M', 'n ',
'O', 'P', 'Q', 'R', 's','t ',
'U', 'V', 'w', 'x', 'y', 'z ',
'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G ',
'H', 'I', 'J', 'k', 'l', 'M', 'n ',
'O', 'P', 'Q', 'R', 's','t ',
'U', 'V', 'w', 'x', 'y', 'z '};
Int [] range = new int [2] {0, 0 };
Random random = new random ();
Switch (Mode)
{
Case randomstringmode. Digital:
For (INT I = 0; I <length; ++ I)
Rndstr + = digitals [random. Next (0, digitals. Length)];
Break;
Case randomstringmode. lowerletter:
For (INT I = 0; I <length; ++ I)
Rndstr + = lowerletters [random. Next (0, lowerletters. Length)];
Break;
Case randomstringmode. upperletter:
For (INT I = 0; I <length; ++ I)
Rndstr + = upperletters [random. Next (0, upperletters. Length)];
Break;
Case randomstringmode. letter:
For (INT I = 0; I <length; ++ I)
Rndstr + = letters [random. Next (0, letters. Length)];
Break;
Default:
For (INT I = 0; I <length; ++ I)
Rndstr + = mix [random. Next (0, mix. Length)];
Break;
}
Return rndstr;
}
Public static void Showcode (int I)
{
Showvalidationcode (Ref I, 4, 20, randomstringmode. upperletter, color. Gold, color. whitesmoke );
}
/// <Summary>
/// Display the verification code
/// </Summary>
/// <Param name = "Seed"> random number secondary seed </param>
/// <Param name = "strlen"> Verification code length </param>
/// <Param name = "fontsize"> font size </param>
/// <Param name = "Mode"> random character mode </param>
/// <Param name = "clrfont"> font color </param>
/// <Param name = "clrbg"> background color </param>
Public static void showvalidationcode (ref int seed, int strlen, int fontsize, randomstringmode mode, color clrfont, color clrbg)
{
Int tmpseed;
Unchecked
{
Tmpseed = (INT) (seed * datetime. Now. ticks );
++ Seed;
}
Random RND = new random (tmpseed );
String text = generaterandomstring (strlen, mode );
Int Height = fontsize * 2;
// Because the width of each font increases after the font is rotated, a bit of compensation width must be added.
Int width = fontsize * text. Length + fontsize/(text. Length-2 );
Bitmap BMP = new Bitmap (width, height );
Graphics graphics = graphics. fromimage (BMP );
Font font = new font ("Courier New", fontsize, fontstyle. Bold );
Brush = new solidbrush (clrfont );
Brush brushbg = new solidbrush (clrbg );
Graphics. fillrectangle (brushbg, 0, 0, width, height );
Bitmap tmpbmp = new Bitmap (height, height );
Graphics tmpgph = NULL;
Int degree = 40;
Point tmppoint = new point ();
For (INT I = 0; I <text. length; I ++)
{
Tmpbmp = new Bitmap (height, height );
Tmpgph = graphics. fromimage (tmpbmp );
// Tmpgph. textrenderinghint = system. Drawing. Text. textrenderinghint. singlebitperpixelgridfit;
// Do not fill in background color, text cleartype effect is gone, why ?!
// Tmpgph. fillrectangle (brushbg, 0, 0, tmpbmp. Width, tmpbmp. Height );
Degree = RND. Next (20, 51); // [20, 50] Random Angle
If (RND. Next (0, 2) = 0)
{
Tmppoint. x = 12; // adjust the text coordinates to adapt to the rotated image
Tmppoint. Y =-6;
}
Else
{
Degree = ~ Degree + 1; // counter-clockwise rotation
Tmppoint. x =-10;
Tmppoint. Y = 6;
}
Tmpgph. rotatetransform (degree );
Tmpgph. drawstring (Text [I]. tostring (), Font, brush, tmppoint );
Graphics. drawimage (tmpbmp, I * fontsize, 0); // splice the image
}
// Output image
System. Io. memorystream = new system. Io. memorystream ();
BMP. Save (memorystream, system. Drawing. imaging. imageformat. GIF );
Httpcontext. Current. response. cache. setcacheability (httpcacheability. nocache );
Httpcontext. Current. response. clearcontent ();
Httpcontext. Current. response. contenttype = "image/GIF ";
Httpcontext. Current. response. binarywrite (memorystream. toarray ());
Httpcontext. Current. response. End ();
// Release resources
Font. Dispose ();
Brush. Dispose ();
Brushbg. Dispose ();
Tmpgph. Dispose ();
Tmpbmp. Dispose ();
Graphics. Dispose ();
BMP. Dispose ();
Memorystream. Dispose ();
}
}