/// <summary> /// 显示验证码 /// </summary> /// <param name="seed">随机数辅助种子</param> /// <param name="strLen">验证码字符长度</param> /// <param name="fontSize">字体大小</param> /// <param name="mode">随机字符模式</param> /// <param name="clrBg">背景颜色</param> public Bitmap ShowValidationCode(int seed, int strLen, int fontSize, RandomStringMode mode, Color clrBg) { Random rnd = new Random(seed); currtext = GenerateRandomString(strLen, mode); int height = 35; int width = 25 * currtext.Length; Bitmap bmp = new Bitmap(width, height); String fontName = "微软雅黑"; Graphics graphics = Graphics.FromImage(bmp); Font font = null;//Courier New int findex = rnd.Next(Fonts.Length - 1); Brush brush = new SolidBrush(Colors[findex]); Brush brushBg = new SolidBrush(clrBg); Brush brushDd = new SolidBrush(Color.Transparent); graphics.FillRectangle(brushBg, 0, 0, width, height); Bitmap tmpBmp = null; Graphics tmpGph = null; int degree = 40; Point tmpPoint = new Point(); for (int i = 0; i < currtext.Length; i++) { tmpBmp = new Bitmap(35, 55); tmpGph = Graphics.FromImage(tmpBmp); tmpGph.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; tmpGph.FillRectangle(brushDd, 0, 0, 35, 55); //填充透明色 //tmpGph.Clear(Color.Transparent); degree = rnd.Next(5, 20); // [20, 50]随机角度 if (rnd.Next(0, 2) == 0) { tmpPoint.X = 4; // 调整文本坐标以适应旋转后的图象 tmpPoint.Y = -2; } else { degree = ~degree + 1; // 逆时针旋转 tmpPoint.X = -4; tmpPoint.Y = 2; } tmpGph.RotateTransform(degree); font = new Font(fontName, rnd.Next(fontSize - 3, fontSize), FontStyle.Regular); tmpGph.DrawString(currtext[i].ToString(), font, brush, tmpPoint); graphics.DrawImage(tmpBmp, 10 + i * 11, 0); // 拼接图象 } //释放资源 font.Dispose(); brush.Dispose(); brushBg.Dispose(); tmpGph.Dispose(); graphics.Dispose(); tmpBmp.Dispose(); return(bmp); }
private static string GenerateRandomString(int length, RandomStringMode mode) { string rndStr = string.Empty; if (length == 0) { return(rndStr); } int[] range = new int[2] { 0, 0 }; switch (mode) { case RandomStringMode.Digital: for (int i = 0; i < length; ++i) { rndStr += digitals[rand.Next(0, digitals.Length)]; } break; case RandomStringMode.LowerLetter: for (int i = 0; i < length; ++i) { rndStr += lowerLetters[rand.Next(0, lowerLetters.Length)]; } break; case RandomStringMode.UpperLetter: for (int i = 0; i < length; ++i) { rndStr += upperLetters[rand.Next(0, upperLetters.Length)]; } break; case RandomStringMode.Letter: for (int i = 0; i < length; ++i) { rndStr += letters[rand.Next(0, letters.Length)]; } break; default: for (int i = 0; i < length; ++i) { rndStr += mix[rand.Next(0, mix.Length)]; } break; } return(rndStr); }
public static string GenerateRandomString(RandomStringMode mode, int minLength, int maxLength) { StringBuilder retVal = new StringBuilder(); int stringLen = Rand.Next(minLength, maxLength); int asciiBase = -1; int sizeOfGenerationSpace = -1; int randomNum = -1; for (int i = 0; i < stringLen; i++) { // 48-57: 0-9, 65-90: A-Z, 97-122: a-z switch (mode) { case RandomStringMode.Numeric: asciiBase = 48; sizeOfGenerationSpace = 10; retVal.Append((char)Rand.Next(asciiBase, asciiBase + sizeOfGenerationSpace)); break; case RandomStringMode.AlphaLowerCase: asciiBase = 97; sizeOfGenerationSpace = 26; retVal.Append((char)Rand.Next(asciiBase, asciiBase + sizeOfGenerationSpace)); break; case RandomStringMode.AlphaLowerCaseUpperCase: asciiBase = 65; sizeOfGenerationSpace = 52; randomNum = Rand.Next(asciiBase, asciiBase + sizeOfGenerationSpace); if (randomNum > 90) { randomNum += 6; } retVal.Append((char)randomNum); break; case RandomStringMode.AlphaNumericLowerCase: asciiBase = 48; sizeOfGenerationSpace = 36; randomNum = Rand.Next(asciiBase, asciiBase + sizeOfGenerationSpace); if (randomNum > 57) { randomNum += 39; } retVal.Append((char)randomNum); break; case RandomStringMode.AlphaNumericLowerCaseUpperCase: asciiBase = 48; sizeOfGenerationSpace = 62; randomNum = Rand.Next(asciiBase, asciiBase + sizeOfGenerationSpace); if (randomNum <= 57) { // do not rebase the number in this case } else if (randomNum > 57 && randomNum < 84) { randomNum += 7; } else { randomNum += 13; } retVal.Append((char)randomNum); break; default: throw new ArgumentException("Invalid RandomStringMode specified"); } } return(retVal.ToString()); }
public static string GenerateRandomString(RandomStringMode mode, int length) { return(GenerateRandomString(mode, length, length)); }
public string GenerateRandomString(int length, RandomStringMode mode) { string rndStr = string.Empty; if (length == 0) { return(rndStr); } //以数组方式候选字符,可以更方便的剔除不要的字符,如数字 0 与字母 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', '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', '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', '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' }; char[] mix = new char[62] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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' }; 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); }
private static string GenerateRandomString(int length, RandomStringMode mode) { string rndStr = string.Empty; if (length == 0) return rndStr; int[] range = new int[2] { 0, 0 }; switch (mode) { case RandomStringMode.Digital: for (int i = 0; i < length; ++i) rndStr += digitals[rand.Next(0, digitals.Length)]; break; case RandomStringMode.LowerLetter: for (int i = 0; i < length; ++i) rndStr += lowerLetters[rand.Next(0, lowerLetters.Length)]; break; case RandomStringMode.UpperLetter: for (int i = 0; i < length; ++i) rndStr += upperLetters[rand.Next(0, upperLetters.Length)]; break; case RandomStringMode.Letter: for (int i = 0; i < length; ++i) rndStr += letters[rand.Next(0, letters.Length)]; break; default: for (int i = 0; i < length; ++i) rndStr += mix[rand.Next(0, mix.Length)]; break; } return rndStr; }
/// <summary> /// 显示验证码 /// </summary> /// <param name="seed">随机数辅助种子</param> /// <param name="strLen">验证码字符长度</param> /// <param name="fontSize">字体大小</param> /// <param name="mode">随机字符模式</param> /// <param name="clrBg">背景颜色</param> public Bitmap ShowValidationCode(int seed, int strLen, int fontSize, RandomStringMode mode, Color clrBg) { Random rnd = new Random(seed); currtext = GenerateRandomString(strLen, mode); int height = 35; int width = 25 * currtext.Length; Bitmap bmp = new Bitmap(width, height); String fontName = "微软雅黑"; Graphics graphics = Graphics.FromImage(bmp); Font font = null;//Courier New int findex = rnd.Next(Fonts.Length - 1); Brush brush = new SolidBrush(Colors[findex]); Brush brushBg = new SolidBrush(clrBg); Brush brushDd = new SolidBrush(Color.Transparent); graphics.FillRectangle(brushBg, 0, 0, width, height); Bitmap tmpBmp = null; Graphics tmpGph = null; int degree = 40; Point tmpPoint = new Point(); for (int i = 0; i < currtext.Length; i++) { tmpBmp = new Bitmap(35, 55); tmpGph = Graphics.FromImage(tmpBmp); tmpGph.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; tmpGph.FillRectangle(brushDd, 0, 0, 35, 55); //填充透明色 //tmpGph.Clear(Color.Transparent); degree = rnd.Next(5, 20); // [20, 50]随机角度 if (rnd.Next(0, 2) == 0) { tmpPoint.X = 4; // 调整文本坐标以适应旋转后的图象 tmpPoint.Y = -2; } else { degree = ~degree + 1; // 逆时针旋转 tmpPoint.X = -4; tmpPoint.Y = 2; } tmpGph.RotateTransform(degree); font = new Font(fontName, rnd.Next(fontSize - 3, fontSize), FontStyle.Regular); tmpGph.DrawString(currtext[i].ToString(), font, brush, tmpPoint); graphics.DrawImage(tmpBmp, 10 + i * 11, 0); // 拼接图象 } //释放资源 font.Dispose(); brush.Dispose(); brushBg.Dispose(); tmpGph.Dispose(); graphics.Dispose(); tmpBmp.Dispose(); return bmp; }
public string GenerateRandomString(int length, RandomStringMode mode) { string rndStr = string.Empty; if (length == 0) return rndStr; //以数组方式候选字符,可以更方便的剔除不要的字符,如数字 0 与字母 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', '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', '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', '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' }; char[] mix = new char[62]{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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' }; 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; }