ValidCode.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.IO;
  5. namespace Muchinfo.MTPClient.UI.Utilities
  6. {
  7. /// <summary>
  8. /// 绘制验证码
  9. /// </summary>
  10. public class ValidCode
  11. {
  12. #region Private Fields
  13. private const double PI = 3.1415926535897932384626433832795;
  14. private const double PI2 = 6.283185307179586476925286766559;
  15. //private readonly int _wordsLen = 4;
  16. private int _len;
  17. private CodeType _codetype;
  18. private readonly Single _jianju = (float)18.0;
  19. private readonly Single _height = (float)40.0;
  20. private string _checkCode;
  21. #endregion
  22. #region Public Property
  23. public string CheckCode
  24. {
  25. get
  26. {
  27. return _checkCode;
  28. }
  29. }
  30. #endregion
  31. #region Constructors
  32. /// <summary>
  33. /// public constructors
  34. /// </summary>
  35. /// <param name="len"> 验证码长度 </param>
  36. /// <param name="ctype"> 验证码类型:字母、数字、字母+ 数字 </param>
  37. public ValidCode(int len, CodeType ctype)
  38. {
  39. this._len = len;
  40. this._codetype = ctype;
  41. }
  42. #endregion
  43. #region Public Field
  44. public enum CodeType { Words, Numbers, Characters, Alphas }
  45. #endregion
  46. #region Private Methods
  47. private string GenerateNumbers()
  48. {
  49. string strOut = "";
  50. System.Random random = new Random();
  51. for (int i = 0; i < _len; i++)
  52. {
  53. string num = Convert.ToString(random.Next(10000) % 10);
  54. strOut += num;
  55. }
  56. return strOut.Trim();
  57. }
  58. private string GenerateCharacters()
  59. {
  60. string strOut = "";
  61. System.Random random = new Random();
  62. for (int i = 0; i < _len; i++)
  63. {
  64. string num = Convert.ToString((char)(65 + random.Next(10000) % 26));
  65. strOut += num;
  66. }
  67. return strOut.Trim();
  68. }
  69. //
  70. private string GenerateAlphas()
  71. {
  72. string strOut = "";
  73. string num = "";
  74. System.Random random = new Random();
  75. for (int i = 0; i < _len; i++)
  76. {
  77. if (random.Next(500) % 2 == 0)
  78. {
  79. num = Convert.ToString(random.Next(10000) % 10);
  80. }
  81. else
  82. {
  83. num = Convert.ToString((char)(65 + random.Next(10000) % 26));
  84. }
  85. strOut += num;
  86. }
  87. return strOut.Trim();
  88. }
  89. private System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
  90. {
  91. System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
  92. // 将位图背景填充为白色
  93. System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);
  94. graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
  95. graph.Dispose();
  96. double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
  97. for (int i = 0; i < destBmp.Width; i++)
  98. {
  99. for (int j = 0; j < destBmp.Height; j++)
  100. {
  101. double dx = 0;
  102. dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
  103. dx += dPhase;
  104. double dy = Math.Sin(dx);
  105. // 取得当前点的颜色
  106. int nOldX = 0, nOldY = 0;
  107. nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
  108. nOldY = bXDir ? j : j + (int)(dy * dMultValue);
  109. System.Drawing.Color color = srcBmp.GetPixel(i, j);
  110. if (nOldX >= 0 && nOldX < destBmp.Width
  111. && nOldY >= 0 && nOldY < destBmp.Height)
  112. {
  113. destBmp.SetPixel(nOldX, nOldY, color);
  114. }
  115. }
  116. }
  117. return destBmp;
  118. }
  119. #endregion
  120. #region Public Methods
  121. public Stream CreateCheckCodeImage()
  122. {
  123. string checkCode;
  124. switch (_codetype)
  125. {
  126. case CodeType.Alphas:
  127. checkCode = GenerateAlphas();
  128. break;
  129. case CodeType.Numbers:
  130. checkCode = GenerateNumbers();
  131. break;
  132. case CodeType.Characters:
  133. checkCode = GenerateCharacters();
  134. break;
  135. default:
  136. checkCode = GenerateAlphas();
  137. break;
  138. }
  139. this._checkCode = checkCode;
  140. MemoryStream ms = null;
  141. //
  142. if (checkCode == null || checkCode.Trim() == String.Empty)
  143. return null;
  144. Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * _jianju)), (int)_height);
  145. Graphics g = Graphics.FromImage(image);
  146. try
  147. {
  148. Random random = new Random();
  149. g.Clear(Color.White);
  150. // 画图片的背景噪音线
  151. for (int i = 0; i < 18; i++)
  152. {
  153. int x1 = random.Next(image.Width);
  154. int x2 = random.Next(image.Width);
  155. int y1 = random.Next(image.Height);
  156. int y2 = random.Next(image.Height);
  157. g.DrawLine(new Pen(Color.FromArgb(random.Next()), 1), x1, y1, x2, y2);
  158. }
  159. Font font = new System.Drawing.Font("Times New Roman", 15, System.Drawing.FontStyle.Bold);
  160. LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
  161. if (_codetype != CodeType.Words)
  162. {
  163. for (int i = 0; i < checkCode.Length; i++)
  164. {
  165. g.DrawString(checkCode.Substring(i, 1), font, brush, 2 + i * _jianju, 1);
  166. }
  167. }
  168. else
  169. {
  170. g.DrawString(checkCode, font, brush, 6, 2);
  171. }
  172. // 画图片的前景噪音点
  173. for (int i = 0; i < 150; i++)
  174. {
  175. int x = random.Next(image.Width);
  176. int y = random.Next(image.Height);
  177. image.SetPixel(x, y, Color.FromArgb(random.Next()));
  178. }
  179. // 画图片的波形滤镜效果
  180. if (_codetype != CodeType.Words)
  181. {
  182. image = TwistImage(image, true, 3, 1);
  183. }
  184. // 画图片的边框线
  185. g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
  186. ms = new System.IO.MemoryStream();
  187. image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
  188. }
  189. finally
  190. {
  191. g.Dispose();
  192. image.Dispose();
  193. }
  194. return ms;
  195. }
  196. #endregion
  197. }
  198. }