You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
10 KiB

  1. using System;
  2. using System.DrawingCore;
  3. using System.DrawingCore.Drawing2D;
  4. using System.DrawingCore.Imaging;
  5. using System.IO;
  6. namespace Shentun.Utilities
  7. {
  8. public sealed class ValidationCodeHelper
  9. {
  10. #region 单例模式
  11. //创建私有化静态obj锁
  12. private static readonly object _lockObject = new object();
  13. //创建私有静态字段,接收类的实例化对象
  14. private static ValidationCodeHelper _validationCodeHelper = null;
  15. //构造函数私有化
  16. private ValidationCodeHelper() { }
  17. //创建单利对象资源并返回
  18. public static ValidationCodeHelper Instance
  19. {
  20. get {
  21. if (_validationCodeHelper == null)
  22. {
  23. lock (_lockObject)
  24. {
  25. if (_validationCodeHelper == null)
  26. _validationCodeHelper = new ValidationCodeHelper();
  27. }
  28. }
  29. return _validationCodeHelper;
  30. }
  31. }
  32. #endregion
  33. #region 生产验证码
  34. /// <summary>
  35. /// 1.数字验证码
  36. /// </summary>
  37. /// <param name="length"></param>
  38. /// <returns></returns>
  39. private string CreateNumberValidationCode(int length)
  40. {
  41. int[] randMembers = new int[length];
  42. int[] validateNums = new int[length];
  43. string validateNumberStr = "";
  44. //生成起始序列值
  45. int seekSeek = unchecked((int)DateTime.Now.Ticks);
  46. Random seekRand = new Random(seekSeek);
  47. int beginSeek = seekRand.Next(0, Int32.MaxValue - length * 10000);
  48. int[] seeks = new int[length];
  49. for (int i = 0; i < length; i++)
  50. {
  51. beginSeek += 10000;
  52. seeks[i] = beginSeek;
  53. }
  54. //生成随机数字
  55. for (int i = 0; i < length; i++)
  56. {
  57. Random rand = new Random(seeks[i]);
  58. int pownum = 1 * (int)Math.Pow(10, length);
  59. randMembers[i] = rand.Next(pownum, Int32.MaxValue);
  60. }
  61. //抽取随机数字
  62. for (int i = 0; i < length; i++)
  63. {
  64. string numStr = randMembers[i].ToString();
  65. int numLength = numStr.Length;
  66. Random rand = new Random();
  67. int numPosition = rand.Next(0, numLength - 1);
  68. validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
  69. }
  70. //生成验证码
  71. for (int i = 0; i < length; i++)
  72. {
  73. validateNumberStr += validateNums[i].ToString();
  74. }
  75. return validateNumberStr;
  76. }
  77. /// <summary>
  78. /// 2.字母验证码
  79. /// </summary>
  80. /// <param name="length">字符长度</param>
  81. /// <returns>验证码字符</returns>
  82. private string CreateAbcValidationCode(int length)
  83. {
  84. char[] validation = new char[length];
  85. 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',
  86. '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'
  87. };
  88. Random random = new Random();
  89. for (int i = 0; i < length; i++)
  90. {
  91. validation[i] = dictionary[random.Next(dictionary.Length - 1)];
  92. }
  93. return new string(validation);
  94. }
  95. /// <summary>
  96. /// 3.混合验证码
  97. /// </summary>
  98. /// <param name="length">字符长度</param>
  99. /// <returns>验证码字符</returns>
  100. private string CreateMixValidationCode(int length)
  101. {
  102. char[] validation = new char[length];
  103. 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',
  104. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  105. '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'
  106. };
  107. Random random = new Random();
  108. for (int i = 0; i < length; i++)
  109. {
  110. validation[i] = dictionary[random.Next(dictionary.Length - 1)];
  111. }
  112. return new string(validation);
  113. }
  114. /// <summary>
  115. /// 产生验证码(随机产生4-6位)
  116. /// </summary>
  117. /// <param name="type">验证码类型:数字,字符,符合</param>
  118. /// <returns></returns>
  119. public string CreateValidationCode(ValidationCodeType type, int length = 4)
  120. {
  121. string validationCode = string.Empty;
  122. Random random = new Random();
  123. switch (type)
  124. {
  125. case ValidationCodeType.NumberValidationCode:
  126. validationCode = Instance.CreateNumberValidationCode(length);
  127. break;
  128. case ValidationCodeType.AbcValidationCode:
  129. validationCode = Instance.CreateAbcValidationCode(length);
  130. break;
  131. case ValidationCodeType.MixValidationCode:
  132. validationCode = Instance.CreateMixValidationCode(length);
  133. break;
  134. }
  135. return validationCode;
  136. }
  137. #endregion
  138. #region 验证码图片
  139. /// <summary>
  140. /// 验证码图片 => Bitmap
  141. /// </summary>
  142. /// <param name="validationCode">验证码</param>
  143. /// <param name="width">宽</param>
  144. /// <param name="height">高</param>
  145. /// <returns>Bitmap</returns>
  146. public Bitmap CreateBitmapByImageValidationCode(string validationCode, int width, int height)
  147. {
  148. Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic));
  149. Brush brush;
  150. Bitmap bitmap = new Bitmap(width, height);
  151. Graphics g = Graphics.FromImage(bitmap);
  152. SizeF totalSizeF = g.MeasureString(validationCode, font);
  153. SizeF curCharSizeF;
  154. PointF startPointF = new PointF(0, (height - totalSizeF.Height) / 2);
  155. Random random = new Random(); //随机数产生器
  156. g.Clear(Color.White); //清空图片背景色
  157. for (int i = 0; i < validationCode.Length; i++)
  158. {
  159. brush = new LinearGradientBrush(new Point(0, 0), new Point(1, 1), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)));
  160. g.DrawString(validationCode[i].ToString(), font, brush, startPointF);
  161. curCharSizeF = g.MeasureString(validationCode[i].ToString(), font);
  162. startPointF.X += curCharSizeF.Width;
  163. }
  164. //画图片的干扰线
  165. for (int i = 0; i < 10; i++)
  166. {
  167. int x1 = random.Next(bitmap.Width);
  168. int x2 = random.Next(bitmap.Width);
  169. int y1 = random.Next(bitmap.Height);
  170. int y2 = random.Next(bitmap.Height);
  171. g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
  172. }
  173. //画图片的前景干扰点
  174. for (int i = 0; i < 100; i++)
  175. {
  176. int x = random.Next(bitmap.Width);
  177. int y = random.Next(bitmap.Height);
  178. bitmap.SetPixel(x, y, Color.FromArgb(random.Next()));
  179. }
  180. g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1); //画图片的边框线
  181. g.Dispose();
  182. return bitmap;
  183. }
  184. /// <summary>
  185. /// 验证码图片 => byte[]
  186. /// </summary>
  187. /// <param name="validationCode">验证码</param>
  188. /// <param name="width">宽</param>
  189. /// <param name="height">高</param>
  190. /// <returns>byte[]</returns>
  191. public byte[] CreateByteByImageValidationCode(string validationCode, int width, int height)
  192. {
  193. Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic));
  194. Brush brush;
  195. Bitmap bitmap = new Bitmap(width, height);
  196. Graphics g = Graphics.FromImage(bitmap);
  197. SizeF totalSizeF = g.MeasureString(validationCode, font);
  198. SizeF curCharSizeF;
  199. PointF startPointF = new PointF(0, (height - totalSizeF.Height) / 2);
  200. Random random = new Random(); //随机数产生器
  201. g.Clear(Color.White); //清空图片背景色
  202. for (int i = 0; i < validationCode.Length; i++)
  203. {
  204. brush = new LinearGradientBrush(new Point(0, 0), new Point(1, 1), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)));
  205. g.DrawString(validationCode[i].ToString(), font, brush, startPointF);
  206. curCharSizeF = g.MeasureString(validationCode[i].ToString(), font);
  207. startPointF.X += curCharSizeF.Width;
  208. }
  209. //画图片的干扰线
  210. for (int i = 0; i < 10; i++)
  211. {
  212. int x1 = random.Next(bitmap.Width);
  213. int x2 = random.Next(bitmap.Width);
  214. int y1 = random.Next(bitmap.Height);
  215. int y2 = random.Next(bitmap.Height);
  216. g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
  217. }
  218. //画图片的前景干扰点
  219. for (int i = 0; i < 100; i++)
  220. {
  221. int x = random.Next(bitmap.Width);
  222. int y = random.Next(bitmap.Height);
  223. bitmap.SetPixel(x, y, Color.FromArgb(random.Next()));
  224. }
  225. g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1); //画图片的边框线
  226. g.Dispose();
  227. //保存图片数据
  228. MemoryStream stream = new MemoryStream();
  229. bitmap.Save(stream, ImageFormat.Jpeg);
  230. byte[] imageByte = stream.ToArray();
  231. //输出图片流
  232. return imageByte;
  233. }
  234. #endregion
  235. }
  236. public enum ValidationCodeType { NumberValidationCode, AbcValidationCode, MixValidationCode };
  237. }