Using System; using System. data; using System. configuration; 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; using System. drawing; using System. text; // <summary> /// summary of VryImgGen /// </summary> public class VryImgGen {public static string ChineseChars = String. empty; // <summary>/ // English and numeric string // </summary> protected static readonly string EnglishOrNumChars = "0123456789 bytes"; public VryImgGen () {rnd = new Random (unchecked (int) dateTime. now. ticks);} // <summary> // global Random number generator /// </summary> private Random rnd; int length = 5; /// <summary> /// verification code Length (default length of 6 Verification codes) /// </summary> public int Length {get {return length;} set {l Ength = value ;}} int fontSize = 18; // <summary> // The font size of the Verification Code (30 pixels by default for displaying the distortion effect) /// </summary> public int FontSize {get {return fontSize;} set {fontSize = value ;}} int padding = 4; /// <summary> /// border population (4 pixels by default) /// </summary> public int Padding {get {return padding ;} set {padding = value ;}} bool chaos = true; // <summary> // whether to output dry points (default output) /// </summary> public bool Chaos {get {Return chaos;} set {chaos = value;} Color chaosColor = Color. lightGray; // <summary> // the Color of the output dry point (gray by default) /// </summary> public Color ChaosColor {get {return chaosColor ;} set {chaosColor = value ;}} Color backgroundColor = Color. white; // <summary> /// custom background Color (White by default) /// </summary> public Color BackgroundColor {get {return backgroundColor ;} set {backgroundColor = value;} Color [] co Lors = {Color. black, Color. red, Color. darkBlue, Color. green, Color. orange, Color. brown, Color. darkCyan, Color. purple}; // <summary> // custom random Color array // </summary> public Color [] Colors {get {return colors ;} set {colors = value ;}} string [] fonts = {"Arial", "Georgia "}; /// <summary> /// Custom font array /// </summary> public string [] Fonts {get {return fonts;} set {fonts = value ;}} # region generation Waveform filter effect private const double PI = 3.1415926535897932384626433832795; private const double PI2 = 6.283185307179586476925286766559; // <summary> // sine curve Wave distorted image (Edit By 51aspx.com) /// </summary> /// <param name = "srcBmp"> image path </param> /// <param name = "bXDir"> If the image is distorted, select true </param> /// <param name = "nMultValue"> waveform amplitude multiple, the greater the distortion, the higher the distortion. Generally, the start phase of the waveform is 3 </param> // <param name = "dPhase">. The value range is [0-2 * PI) </param> /// <ret Urns> </returns> public System. drawing. bitmap TwistImage (Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase) {System. drawing. bitmap destBmp = new Bitmap (srcBmp. width, srcBmp. height); // fill the bitmap background with white System. drawing. graphics graph = System. drawing. graphics. fromImage (destBmp); graph. fillRectangle (new SolidBrush (System. drawing. color. white), 0, 0, destBmp. width, destBmp. height); graph. dispose (); Double dBaseAxisLen = bXDir? (Double) destBmp. height: (double) destBmp. width; for (int I = 0; I <destBmp. width; I ++) {for (int j = 0; j <destBmp. height; j ++) {double dx = 0; dx = bXDir? (PI2 * (double) j)/dBaseAxisLen: (PI2 * (double) I)/dBaseAxisLen; dx + = dPhase; double dy = Math. sin (dx); // obtain the color of the current vertex int nOldX = 0, nOldY = 0; nOldX = bXDir? I + (int) (dy * dMultValue): I; nOldY = bXDir? J: j + (int) (dy * dMultValue); System. drawing. color color = srcBmp. getPixel (I, j); if (nOldX> = 0 & nOldX <destBmp. width & nOldY> = 0 & nOldY <destBmp. height) {destBmp. setPixel (nOldX, nOldY, color) ;}} return destBmp ;} # endregion // <summary> // generate the verification code image // </summary> // <param name = "code"> Verification code </param> /// <returns> </returns> public Bitmap CreateImage (string code) {int fSize = FontSize; int fWidth = fSize + Padding; int imageWidth = (int) (code. length * fWidth) + 4 + Padding * 2; int imageHeight = fSizeUsing System; using System. data; using System. configuration; 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; using System. drawing; using System. text; // <summary> /// summary of VryImgGen /// </summary> public class VryImgGen {public static string ChineseChars = String. empty; // <summary>/ // English and numeric string // </summary> protected static readonly string EnglishOrNumChars = "0123456789 bytes"; public VryImgGen () {rnd = new Random (unchecked (int) dateTime. now. ticks);} // <summary> // global Random number generator /// </summary> private Random rnd; int length = 5; /// <summary> /// verification code Length (default length of 6 Verification codes) /// </summary> public int Length {get {return length;} set {l Ength = value ;}} int fontSize = 18; // <summary> // The font size of the Verification Code (30 pixels by default for displaying the distortion effect) /// </summary> public int FontSize {get {return fontSize;} set {fontSize = value ;}} int padding = 4; /// <summary> /// border population (4 pixels by default) /// </summary> public int Padding {get {return padding ;} set {padding = value ;}} bool chaos = true; // <summary> // whether to output dry points (default output) /// </summary> public bool Chaos {get {Return chaos;} set {chaos = value;} Color chaosColor = Color. lightGray; // <summary> // the Color of the output dry point (gray by default) /// </summary> public Color ChaosColor {get {return chaosColor ;} set {chaosColor = value ;}} Color backgroundColor = Color. white; // <summary> /// custom background Color (White by default) /// </summary> public Color BackgroundColor {get {return backgroundColor ;} set {backgroundColor = value;} Color [] co Lors = {Color. black, Color. red, Color. darkBlue, Color. green, Color. orange, Color. brown, Color. darkCyan, Color. purple}; // <summary> // custom random Color array // </summary> public Color [] Colors {get {return colors ;} set {colors = value ;}} string [] fonts = {"Arial", "Georgia "}; /// <summary> /// Custom font array /// </summary> public string [] Fonts {get {return fonts;} set {fonts = value ;}} # region generation Waveform filter effect private const double PI = 3.1415926535897932384626433832795; private const double PI2 = 6.283185307179586476925286766559; // <summary> // sine curve Wave distorted image (Edit By 51aspx.com) /// </summary> /// <param name = "srcBmp"> image path </param> /// <param name = "bXDir"> If the image is distorted, select true </param> /// <param name = "nMultValue"> waveform amplitude multiple, the greater the distortion, the higher the distortion. Generally, the start phase of the waveform is 3 </param> // <param name = "dPhase">. The value range is [0-2 * PI) </param> /// <ret Urns> </returns> public System. drawing. bitmap TwistImage (Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase) {System. drawing. bitmap destBmp = new Bitmap (srcBmp. width, srcBmp. height); // fill the bitmap background with white System. drawing. graphics graph = System. drawing. graphics. fromImage (destBmp); graph. fillRectangle (new SolidBrush (System. drawing. color. white), 0, 0, destBmp. width, destBmp. height); graph. dispose (); Double dBaseAxisLen = bXDir? (Double) destBmp. height: (double) destBmp. width; for (int I = 0; I <destBmp. width; I ++) {for (int j = 0; j <destBmp. height; j ++) {double dx = 0; dx = bXDir? (PI2 * (double) j)/dBaseAxisLen: (PI2 * (double) I)/dBaseAxisLen; dx + = dPhase; double dy = Math. sin (dx); // obtain the color of the current vertex int nOldX = 0, nOldY = 0; nOldX = bXDir? I + (int) (dy * dMultValue): I; nOldY = bXDir? J: j + (int) (dy * dMultValue); System. drawing. color color = srcBmp. getPixel (I, j); if (nOldX> = 0 & nOldX <destBmp. width & nOldY> = 0 & nOldY <destBmp. height) {destBmp. setPixel (nOldX, nOldY, color) ;}} return destBmp ;} # endregion // <summary> // generate the verification code image // </summary> // <param name = "code"> Verification code </param> /// <returns> </returns> public Bitmap CreateImage (string code) {int fSize = FontSize; int fWidth = fSize + Padding; int imageWidth = (int) (code. length * fWidth) + 4 + Padding * 2; int imageHeight = fSize * 2 + Padding * 2; System. drawing. bitmap image = new System. drawing. bitmap (imageWidth, imageHeight); Graphics g = Graphics. fromImage (image); g. clear (BackgroundColor); // Add randomly generated dry points to the background if (this. chaos) {Pen pen = new Pen (ChaosColor, 0); int c = Length * 10; for (int I = 0; I <c; I ++) {int x = rnd. next (image. width); int y = rnd. next (image. height); g. drawRectangle (pen, x, y, 1, 1) ;}} int left = 0, top = 0, top1 = 1, top2 = 1; int n1 = (imageHeight-FontSize-Padding * 2); int n2 = n1/4; top1 = n2; top2 = n2 * 2; Font f; Brush B; int cindex, findex; // the character of the random font and color verification code for (int I = 0; I <code. length; I ++) {cindex = rnd. next (Colors. length-1); findex = rnd. next (Fonts. length-1); f = new System. drawing. font (Fonts [findex], fSize, System. drawing. fontStyle. bold); B = new System. drawing. solidBrush (Colors [cindex]); if (I % 2 = 1) {top = top2;} else {top = top1;} left = I * fWidth; g. drawString (code. substring (I, 1), f, B, left, top);} // draw a border Color. gainsboro g. drawRectangle (new Pen (Color. gainsboro, 0), 0, 0, image. width-1, image. height-1); g. dispose (); // generate a waveform (Add By 51aspx.com) image = TwistImage (image, false, 0, 4); return image ;} /// <summary> /// generate a random escape code /// </summary> /// <param name = "codeLen"> String Length </param> /// <param name = "zhCharsCount"> Number of Chinese characters </param> // <returns> </returns> public string CreateVerifyCode (int codeLen, int zhCharsCount) {char [] chs = new char [codeLen]; int index; for (int I = 0; I <zhCharsCount; I ++) {index = rnd. next (0, codeLen); if (chs [index] = '\ 0') chs [index] = CreateZhChar (); else -- I ;} for (int I = 0; I <codeLen; I ++) {if (chs [I] = '\ 0') chs [I] = CreateEnOrNumChar ();} return new string (chs, 0, chs. length );} /// <summary> /// generate a random escape code with the default length of 5 /// </summary> /// <returns> </returns> public string CreateVerifyCode () {return CreateVerifyCode (Length, 0 );} /// <summary> /// generate English or numeric characters /// </summary> /// <returns> </returns> protected char CreateEnOrNumChar () {return EnglishOrNumChars [rnd. next (0, EnglishOrNumChars. length)] ;}/// <summary> /// generate Chinese characters /// </summary> /// <returns> </returns> protected char CreateZhChar () {// if a Chinese character set is provided, select if (ChineseChars. length> 0) {return ChineseChars [rnd. next (0, ChineseChars. length)];} // if no Chinese Character Set is provided, else {byte [] bytes = new byte [2] is constructed based on the encoding rules of GB2312 simplified Chinese character encoding table. // The value of the first byte is between 0xb0 and 0xf7. bytes [0] = (byte) rnd. next (0xb0, 0xf8); // the value of the second byte is between 0xa1 and 0xfe. bytes [1] = (byte) rnd. next (0xa1, 0xff); // decodes the Chinese character string str1 = Encoding Based on the byte array encoded by Chinese characters. getEncoding ("gb2312 "). getString (bytes); return str1 [0] ;}}* 2 + Padding * 2;
System. Drawing. Bitmap image = new System. Drawing. Bitmap (imageWidth, imageHeight );
Graphics g = Graphics. FromImage (image );
G. Clear (BackgroundColor );
// Add random dry points to the background
If (this. Chaos)
{
Pen pen = new Pen (ChaosColor, 0 );
Int c = Length * 10;
For (int I = 0; I <c; I ++)
{
Int x = rnd. Next (image. Width );
Int y = rnd. Next (image. Height );
G. DrawRectangle (pen, x, y, 1, 1 );
}
}
Int left = 0, top = 0, top1 = 1, top2 = 1;
Int n1 = (imageHeight-FontSize-Padding * 2 );
Int n2 = n1/4;
Top1 = n2;
Top2 = n2 * 2;
Font f;
Brush B;
Int cindex, findex;
// Random font and color Verification Code characters
For (int I = 0; I <code. Length; I ++)
{
Cindex = rnd. Next (Colors. Length-1 );
Findex = rnd. Next (Fonts. Length-1 );
F = new System. Drawing. Font (Fonts [findex], fSize, System. Drawing. FontStyle. Bold );
B = new System. Drawing. SolidBrush (Colors [cindex]);
If (I % 2 = 1)
{
Top = top2;
}
Else
{
Top = top1;
}
Left = I * fWidth;
G. DrawString (code. Substring (I, 1), f, B, left, top );
}
// Draw a border with Color. Gainsboro
G. DrawRectangle (new Pen (Color. Gainsboro, 0), 0, 0, image. Width-1, image. Height-1 );
G. Dispose ();
// Generate a waveform (Add By 51aspx.com)
Image = TwistImage (image, false, 0, 4 );
Return image;
}
/// <Summary>
/// Generate a random escape code
/// </Summary>
/// <Param name = "codeLen"> String Length </param>
/// <Param name = "zhCharsCount"> Number of Chinese characters </param>
/// <Returns> </returns>
Public string CreateVerifyCode (int codeLen, int zhCharsCount)
{
Char [] chs = new char [codeLen];
Int index;
For (int I = 0; I <zhCharsCount; I ++)
{
Index = rnd. Next (0, codeLen );
If (chs [index] = '\ 0 ')
Chs [index] = CreateZhChar ();
Else
-- I;
}
For (int I = 0; I <codeLen; I ++)
{
If (chs [I] = '\ 0 ')
Chs [I] = CreateEnOrNumChar ();
}
Return new string (chs, 0, chs. Length );
}
/// <Summary>
/// Generate a random escape code with the default length of 5
/// </Summary>
/// <Returns> </returns>
Public string CreateVerifyCode ()
{
Return CreateVerifyCode (Length, 0 );
}
/// <Summary>
/// Generate English or numeric characters
/// </Summary>
/// <Returns> </returns>
Protected char CreateEnOrNumChar ()
{
Return EnglishOrNumChars [rnd. Next (0, EnglishOrNumChars. Length)];
}
/// <Summary>
/// Generate Chinese Characters
/// </Summary>
/// <Returns> </returns>
Protected char CreateZhChar ()
{
// If a Chinese character set is provided, select a Chinese character from the Chinese character set.
If (ChineseChars. Length> 0)
{
Return ChineseChars [rnd. Next (0, ChineseChars. Length)];
}
// If the Chinese character set is not provided, the Chinese characters are constructed according to the encoding rules of GB2312 simplified Chinese character encoding table
Else
{
Byte [] bytes = new byte [2];
// The value of the first byte is between 0xb0 and 0xf7.
Bytes [0] = (byte) rnd. Next (0xb0, 0xf8 );
// The second byte value is between 0xa1 and 0xfe.
Bytes [1] = (byte) rnd. Next (0xa1, 0xff );
// Decodes Chinese Characters Based on the byte array encoded by Chinese Characters
String str1 = Encoding. GetEncoding ("gb2312"). GetString (bytes );
Return str1 [0];
}
}
}
Technorati label: ASP. NET Verification Code