VerifyCodeHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System;
  2. using System.DrawingCore;
  3. using System.DrawingCore.Drawing2D;
  4. using System.DrawingCore.Imaging;
  5. using System.IO;
  6. namespace MySystem
  7. {
  8. public class VerifyCodeHelper
  9. {
  10. #region 单例模式
  11. //创建私有化静态obj锁
  12. private static readonly object _ObjLock = new object();
  13. //创建私有静态字段,接收类的实例化对象
  14. private static VerifyCodeHelper _VerifyCodeHelper = null;
  15. //构造函数私有化
  16. private VerifyCodeHelper() { }
  17. //创建单利对象资源并返回
  18. public static VerifyCodeHelper GetSingleObj()
  19. {
  20. if (_VerifyCodeHelper == null)
  21. {
  22. lock (_ObjLock)
  23. {
  24. if (_VerifyCodeHelper == null)
  25. _VerifyCodeHelper = new VerifyCodeHelper();
  26. }
  27. }
  28. return _VerifyCodeHelper;
  29. }
  30. #endregion
  31. #region 生产验证码
  32. public enum VerifyCodeType { NumberVerifyCode, AbcVerifyCode, MixVerifyCode };
  33. /// <summary>
  34. /// 1.数字验证码
  35. /// </summary>
  36. /// <param name="length"></param>
  37. /// <returns></returns>
  38. private string CreateNumberVerifyCode(int length)
  39. {
  40. int[] randMembers = new int[length];
  41. int[] validateNums = new int[length];
  42. string validateNumberStr = "";
  43. //生成起始序列值
  44. int seekSeek = unchecked((int)DateTime.Now.Ticks);
  45. Random seekRand = new Random(seekSeek);
  46. int beginSeek = seekRand.Next(0, Int32.MaxValue - length * 10000);
  47. int[] seeks = new int[length];
  48. for (int i = 0; i < length; i++)
  49. {
  50. beginSeek += 10000;
  51. seeks[i] = beginSeek;
  52. }
  53. //生成随机数字
  54. for (int i = 0; i < length; i++)
  55. {
  56. Random rand = new Random(seeks[i]);
  57. int pownum = 1 * (int)Math.Pow(10, length);
  58. randMembers[i] = rand.Next(pownum, Int32.MaxValue);
  59. }
  60. //抽取随机数字
  61. for (int i = 0; i < length; i++)
  62. {
  63. string numStr = randMembers[i].ToString();
  64. int numLength = numStr.Length;
  65. Random rand = new Random();
  66. int numPosition = rand.Next(0, numLength - 1);
  67. validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
  68. }
  69. //生成验证码
  70. for (int i = 0; i < length; i++)
  71. {
  72. validateNumberStr += validateNums[i].ToString();
  73. }
  74. return validateNumberStr;
  75. }
  76. /// <summary>
  77. /// 2.字母验证码
  78. /// </summary>
  79. /// <param name="length">字符长度</param>
  80. /// <returns>验证码字符</returns>
  81. private string CreateAbcVerifyCode(int length)
  82. {
  83. char[] verification = new char[length];
  84. 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',
  85. '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. };
  87. Random random = new Random();
  88. for (int i = 0; i < length; i++)
  89. {
  90. verification[i] = dictionary[random.Next(dictionary.Length - 1)];
  91. }
  92. return new string(verification);
  93. }
  94. /// <summary>
  95. /// 3.混合验证码
  96. /// </summary>
  97. /// <param name="length">字符长度</param>
  98. /// <returns>验证码字符</returns>
  99. private string CreateMixVerifyCode(int length)
  100. {
  101. char[] verification = new char[length];
  102. 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',
  103. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  104. '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'
  105. };
  106. Random random = new Random();
  107. for (int i = 0; i < length; i++)
  108. {
  109. verification[i] = dictionary[random.Next(dictionary.Length - 1)];
  110. }
  111. return new string(verification);
  112. }
  113. /// <summary>
  114. /// 产生验证码(随机产生4-6位)
  115. /// </summary>
  116. /// <param name="type">验证码类型:数字,字符,符合</param>
  117. /// <returns></returns>
  118. public string CreateVerifyCode(VerifyCodeType type)
  119. {
  120. string verifyCode = string.Empty;
  121. Random random = new Random();
  122. int length = random.Next(4, 6);
  123. switch (type)
  124. {
  125. case VerifyCodeType.NumberVerifyCode:
  126. verifyCode = GetSingleObj().CreateNumberVerifyCode(length);
  127. break;
  128. case VerifyCodeType.AbcVerifyCode:
  129. verifyCode = GetSingleObj().CreateAbcVerifyCode(length);
  130. break;
  131. case VerifyCodeType.MixVerifyCode:
  132. verifyCode = GetSingleObj().CreateMixVerifyCode(length);
  133. break;
  134. }
  135. return verifyCode;
  136. }
  137. #endregion
  138. #region 验证码图片
  139. /// <summary>
  140. /// 验证码图片 => Bitmap
  141. /// </summary>
  142. /// <param name="verifyCode">验证码</param>
  143. /// <param name="width">宽</param>
  144. /// <param name="height">高</param>
  145. /// <returns>Bitmap</returns>
  146. public Bitmap CreateBitmapByImgVerifyCode(string verifyCode, int width, int height)
  147. {
  148. Font font = new Font("Arial", 18, (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(verifyCode, 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 < verifyCode.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(verifyCode[i].ToString(), font, brush, startPointF);
  161. curCharSizeF = g.MeasureString(verifyCode[i].ToString(), font);
  162. startPointF.X += curCharSizeF.Width*1.5f;
  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="verifyCode">验证码</param>
  188. /// <param name="width">宽</param>
  189. /// <param name="height">高</param>
  190. /// <returns>byte[]</returns>
  191. public byte[] CreateByteByImgVerifyCode(string verifyCode, 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(verifyCode, 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 < verifyCode.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(verifyCode[i].ToString(), font, brush, startPointF);
  206. curCharSizeF = g.MeasureString(verifyCode[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. //输出图片流
  231. return stream.ToArray();
  232. }
  233. #endregion
  234. }
  235. }