/// <Summary> /// obtain the verification code [String] /// </summary> /// <param name = "Length"> Verification code Length [must be greater than 0] </param> // <returns> </returns> public static string VerficationText (int Length) {char [] _ verfication = new char [Length]; Random _ random = new Random (); char [] _ dictionary = {'A', 'B ', 'C', 'D', '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', 'F ', 'G', 'h', 'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P ', 'Q', 'R', 's', 't', 'U', 'V', 'w', 'x', 'y', 'z ', '0', '1', '2', '3', '4', '5', '6', '7', '8 ', '9'}; for (int I = 0; I <Length; I ++) {_ verfication [I] = _ dictionary [_ random. next (_ dictionary. length-1)];} return new string (_ verfication );}
/// <Summary> /// method for generating the verification code /// </summary> /// <returns> </returns> public void VerificationCode () {int _ verificationLength = 6; int _ width = 100, _ height = 20; SizeF _ verificationTextSize; string path = Server. mapPath ("~ /Skins/Commmon/Texture.jpg "); // use a background image Bitmap _ bitmap = new Bitmap (path); TextureBrush _ brush = new TextureBrush (_ bitmap ); // obtain the verification code string _ verificationText = VerficationText (_ verificationLength); // Save the verification code Session ["VerificationCode"] = _ verificationText. toUpper (); Font _ font = new Font ("Arial", 14, FontStyle. bold); Bitmap _ image = new Bitmap (_ width, _ height); Graphics _ g = Graphics. fromImage (_ image); // clear the background color _ g. clear (Color. white); // draw the verification code _ verificationTextSize = _ g. measureString (_ verificationText, _ font); _ g. drawString (_ verificationText, _ font, _ brush, (_ width-_ verificationTextSize. width)/2, (_ height-_ verificationTextSize. height)/2); _ image. save (Response. outputStream, System. drawing. imaging. imageFormat. jpeg );}