CaptchaHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. //############################################################
  2. // https://github.com/yuzhengyang
  3. // author:yuzhengyang
  4. //############################################################
  5. using System;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Drawing.Imaging;
  9. using System.IO;
  10. using System.Text;
  11. namespace Azylee.Core.IOUtils.ImageUtils
  12. {
  13. /// <summary>
  14. /// Captcha
  15. /// </summary>
  16. public class CaptchaHelper
  17. {
  18. #region Private Field
  19. /// <summary>
  20. /// 随机种子
  21. /// </summary>
  22. private Random objRandom = new Random();
  23. #endregion
  24. #region Public Property
  25. #region 验证码长度
  26. /// <summary>
  27. /// 验证码长度
  28. /// </summary>
  29. private int length = 4;
  30. /// <summary>
  31. /// 验证码长度(默认为4)
  32. /// </summary>
  33. public int Length
  34. {
  35. get { return this.length; }
  36. set { this.length = value; }
  37. }
  38. #endregion
  39. #region 验证码字符串
  40. /// <summary>
  41. /// 验证码字符串
  42. /// </summary>
  43. private string verifyCodeText = null;
  44. /// <summary>
  45. /// 验证码字符串
  46. /// </summary>
  47. public string VerifyCodeText
  48. {
  49. get { return this.verifyCodeText; }
  50. set { this.verifyCodeText = value; }
  51. }
  52. #endregion
  53. #region 是否加入小写字母
  54. /// <summary>
  55. /// 是否加入小写字母
  56. /// </summary>
  57. private bool addLowerLetter = false;
  58. /// <summary>
  59. /// 是否加入小写字母(不包括o)
  60. /// </summary>
  61. public bool AddLowerLetter
  62. {
  63. get { return this.addLowerLetter; }
  64. set { this.addLowerLetter = value; }
  65. }
  66. #endregion
  67. #region 是否加入大写字母
  68. /// <summary>
  69. /// 是否加入大写字母
  70. /// </summary>
  71. private bool addUpperLetter = false;
  72. /// <summary>
  73. /// 是否加入大写字母(不包括O)
  74. /// </summary>
  75. public bool AddUpperLetter
  76. {
  77. get { return this.addUpperLetter; }
  78. set { this.addUpperLetter = value; }
  79. }
  80. #endregion
  81. #region 字体大小
  82. /// <summary>
  83. /// 字体大小
  84. /// </summary>
  85. private int fontSize = 18;
  86. /// <summary>
  87. /// 字体大小(默认为18)
  88. /// </summary>
  89. public int FontSize
  90. {
  91. get { return this.fontSize; }
  92. set { this.fontSize = value; }
  93. }
  94. #endregion
  95. #region 字体颜色
  96. /// <summary>
  97. /// 字体颜色
  98. /// </summary>
  99. private Color fontColor = Color.Blue;
  100. /// <summary>
  101. /// 字体颜色(默认为Blue)
  102. /// </summary>
  103. public Color FontColor
  104. {
  105. get { return this.fontColor; }
  106. set { this.fontColor = value; }
  107. }
  108. #endregion
  109. #region 字体类型
  110. /// <summary>
  111. /// 字体类型
  112. /// </summary>
  113. private string fontFamily = "Verdana";
  114. /// <summary>
  115. /// 字体类型(默认为Verdana)
  116. /// </summary>
  117. public string FontFamily
  118. {
  119. get { return this.fontFamily; }
  120. set { this.fontFamily = value; }
  121. }
  122. #endregion
  123. #region 背景色
  124. /// <summary>
  125. /// 背景色
  126. /// </summary>
  127. private Color backgroundColor = Color.AliceBlue;
  128. /// <summary>
  129. /// 背景色(默认为AliceBlue)
  130. /// </summary>
  131. public Color BackgroundColor
  132. {
  133. get { return this.backgroundColor; }
  134. set { this.backgroundColor = value; }
  135. }
  136. #endregion
  137. #region 前景噪点数量
  138. /// <summary>
  139. /// 前景噪点数量
  140. /// </summary>
  141. private int foreNoisePointCount = 2;
  142. /// <summary>
  143. /// 前景噪点数量(默认为2)
  144. /// </summary>
  145. public int ForeNoisePointCount
  146. {
  147. get { return this.foreNoisePointCount; }
  148. set { this.foreNoisePointCount = value; }
  149. }
  150. #endregion
  151. #region 随机码的旋转角度
  152. /// <summary>
  153. /// 随机码的旋转角度
  154. /// </summary>
  155. private int randomAngle = 45;
  156. /// <summary>
  157. /// 随机码的旋转角度(默认为40度)
  158. /// </summary>
  159. public int RandomAngle
  160. {
  161. get { return this.randomAngle; }
  162. set { this.randomAngle = value; }
  163. }
  164. #endregion
  165. #endregion
  166. #region Constructor Method
  167. /// <summary>
  168. /// 构造方法
  169. /// </summary>
  170. public CaptchaHelper()
  171. {
  172. this.GetText();
  173. }
  174. #endregion
  175. #region Private Method
  176. /// <summary>
  177. /// 得到验证码字符串
  178. /// </summary>
  179. private void GetText()
  180. {
  181. //没有外部输入验证码时随机生成
  182. if (String.IsNullOrEmpty(this.verifyCodeText))
  183. {
  184. StringBuilder objStringBuilder = new StringBuilder();
  185. //加入数字1-9
  186. for (int i = 1; i <= 9; i++)
  187. {
  188. objStringBuilder.Append(i.ToString());
  189. }
  190. //加入大写字母A-Z,不包括O
  191. if (this.addUpperLetter)
  192. {
  193. char temp = ' ';
  194. for (int i = 0; i < 26; i++)
  195. {
  196. temp = Convert.ToChar(i + 65);
  197. //如果生成的字母不是'O'
  198. if (!temp.Equals('O'))
  199. {
  200. objStringBuilder.Append(temp);
  201. }
  202. }
  203. }
  204. //加入小写字母a-z,不包括o
  205. if (this.addLowerLetter)
  206. {
  207. char temp = ' ';
  208. for (int i = 0; i < 26; i++)
  209. {
  210. temp = Convert.ToChar(i + 97);
  211. //如果生成的字母不是'o'
  212. if (!temp.Equals('o'))
  213. {
  214. objStringBuilder.Append(temp);
  215. }
  216. }
  217. }
  218. //生成验证码字符串
  219. {
  220. int index = 0;
  221. for (int i = 0; i < length; i++)
  222. {
  223. index = objRandom.Next(0, objStringBuilder.Length);
  224. this.verifyCodeText += objStringBuilder[index];
  225. objStringBuilder.Remove(index, 1);
  226. }
  227. }
  228. }
  229. }
  230. /// <summary>
  231. /// 得到验证码图片
  232. /// </summary>
  233. private Bitmap GetImage()
  234. {
  235. Bitmap result = null;
  236. //创建绘图
  237. result = new Bitmap(this.verifyCodeText.Length * 16, 25);
  238. using (Graphics objGraphics = Graphics.FromImage(result))
  239. {
  240. objGraphics.SmoothingMode = SmoothingMode.HighQuality;
  241. //清除整个绘图面并以指定背景色填充
  242. objGraphics.Clear(this.backgroundColor);
  243. //创建画笔
  244. using (SolidBrush objSolidBrush = new SolidBrush(this.fontColor))
  245. {
  246. this.AddForeNoisePoint(result);
  247. this.AddBackgroundNoisePoint(result, objGraphics);
  248. //文字居中
  249. StringFormat objStringFormat = new StringFormat(StringFormatFlags.NoClip);
  250. objStringFormat.Alignment = StringAlignment.Center;
  251. objStringFormat.LineAlignment = StringAlignment.Center;
  252. //字体样式
  253. Font objFont = new Font(this.fontFamily, objRandom.Next(this.fontSize - 3, this.fontSize), FontStyle.Regular);
  254. //验证码旋转,防止机器识别
  255. char[] chars = this.verifyCodeText.ToCharArray();
  256. for (int i = 0; i < chars.Length; i++)
  257. {
  258. //转动的度数
  259. float angle = objRandom.Next(-this.randomAngle, this.randomAngle);
  260. objGraphics.TranslateTransform(12, 12);
  261. objGraphics.RotateTransform(angle);
  262. objGraphics.DrawString(chars[i].ToString(), objFont, objSolidBrush, -2, 2, objStringFormat);
  263. objGraphics.RotateTransform(-angle);
  264. objGraphics.TranslateTransform(2, -12);
  265. }
  266. }
  267. }
  268. return result;
  269. }
  270. /// <summary>
  271. /// 添加前景噪点
  272. /// </summary>
  273. /// <param name="objBitmap"></param>
  274. private void AddForeNoisePoint(Bitmap objBitmap)
  275. {
  276. for (int i = 0; i < objBitmap.Width * this.foreNoisePointCount; i++)
  277. {
  278. objBitmap.SetPixel(objRandom.Next(objBitmap.Width), objRandom.Next(objBitmap.Height), this.fontColor);
  279. }
  280. }
  281. /// <summary>
  282. /// 添加背景噪点
  283. /// </summary>
  284. /// <param name="objBitmap"></param>
  285. /// <param name="objGraphics"></param>
  286. private void AddBackgroundNoisePoint(Bitmap objBitmap, Graphics objGraphics)
  287. {
  288. using (Pen objPen = new Pen(Color.Azure, 0))
  289. {
  290. for (int i = 0; i < objBitmap.Width * 2; i++)
  291. {
  292. objGraphics.DrawRectangle(objPen, objRandom.Next(objBitmap.Width), objRandom.Next(objBitmap.Height), 1, 1);
  293. }
  294. }
  295. }
  296. #endregion
  297. #region Public Method
  298. //public void Output(HttpResponse objHttpResponse)
  299. //{
  300. // using (Bitmap objBitmap = this.GetImage())
  301. // {
  302. // if (objBitmap != null)
  303. // {
  304. // using (MemoryStream objMS = new MemoryStream())
  305. // {
  306. // objBitmap.Save(objMS, ImageFormat.Jpeg);
  307. // HttpContext.Current.Response.ClearContent();
  308. // HttpContext.Current.Response.ContentType = "image/Jpeg";
  309. // HttpContext.Current.Response.BinaryWrite(objMS.ToArray());
  310. // HttpContext.Current.Response.Flush();
  311. // HttpContext.Current.Response.End();
  312. // }
  313. // }
  314. // }
  315. //}
  316. public void Output(string savePath)
  317. {
  318. using (Bitmap objBitmap = this.GetImage())
  319. {
  320. if (objBitmap != null)
  321. {
  322. objBitmap.Save(savePath, ImageFormat.Jpeg);
  323. }
  324. }
  325. }
  326. #endregion
  327. }
  328. }