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.

164 lines
5.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Web;
  9. using DBUtility;
  10. using OT_Web.Ap_Code;
  11. using OT.Web.Ap_Code;
  12. namespace OT_Web
  13. {
  14. /// <summary>
  15. ///Common 的摘要描述
  16. /// </summary>
  17. public class GetImg : IHttpHandler
  18. {
  19. //Add by Ethan, 2014/10/15 Start. 處理圖片路徑變更
  20. private static string gstrFileWebRoot = "";
  21. static GetImg()
  22. {
  23. gstrFileWebRoot = (DbHelperSQL.GetSingle(" SELECT SettingValue FROM dbo.OTB_SYS_SystemSetting WHERE SettingItem='DmImgWebPath' ") ?? "").ToString();
  24. }
  25. //Add by Ethan, 2014/10/15 End. 處理圖片路徑變更
  26. /// <summary>
  27. /// 生成驗證碼字符串
  28. /// </summary>
  29. /// <param name="codeLen">驗證碼字符長度</param>
  30. /// <returns>返回驗證碼字符串</returns>
  31. private string MakeCode(int codeLen)
  32. {
  33. if (codeLen < 1)
  34. {
  35. return string.Empty;
  36. }
  37. int number;
  38. string checkCode = string.Empty;
  39. Random random = new Random();
  40. for (int index = 0; index < codeLen; index++)
  41. {
  42. number = random.Next();
  43. if (number % 2 == 0)
  44. {
  45. checkCode += (char)('0' + (char)(number % 10)); //生成數字
  46. }
  47. else
  48. {
  49. checkCode += (char)('A' + (char)(number % 26)); //生成字母
  50. }
  51. }
  52. return checkCode;
  53. }
  54. // ///<summary>
  55. /// 獲取驗證碼圖片流
  56. /// </summary>
  57. /// <param name="checkCode">驗證碼字符串</param>
  58. /// <returns>返回驗證碼圖片流</returns>
  59. private MemoryStream CreateCodeImg(string checkCode)
  60. {
  61. if (string.IsNullOrEmpty(checkCode))
  62. {
  63. return null;
  64. }
  65. Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
  66. Graphics graphic = Graphics.FromImage(image);
  67. try
  68. {
  69. Random random = new Random();
  70. graphic.Clear(Color.White);
  71. int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  72. for (int index = 0; index < 25; index++)
  73. {
  74. x1 = random.Next(image.Width);
  75. x2 = random.Next(image.Width);
  76. y1 = random.Next(image.Height);
  77. y2 = random.Next(image.Height);
  78. graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
  79. }
  80. Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
  81. LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true);
  82. graphic.DrawString(checkCode, font, brush, 2, 2);
  83. int x = 0;
  84. int y = 0;
  85. //畫圖片的前景噪音點
  86. for (int i = 0; i < 100; i++)
  87. {
  88. x = random.Next(image.Width);
  89. y = random.Next(image.Height);
  90. image.SetPixel(x, y, Color.FromArgb(random.Next()));
  91. }
  92. //畫圖片的邊框線
  93. graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
  94. //將圖片驗證碼保存為流Stream返回
  95. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  96. image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
  97. return ms;
  98. }
  99. finally
  100. {
  101. graphic.Dispose();
  102. image.Dispose();
  103. }
  104. }
  105. public void ProcessRequest(HttpContext context)
  106. {
  107. try
  108. {
  109. string strFilePath = SecurityUtil.DESDecrypt(context.Request["F"].Replace(" ", "+"), "1234567890123456", "1234567890123456");
  110. //Add by Ethan, 2014/10/15 Start. 處理圖片路徑變更
  111. if (strFilePath != null && !File.Exists(strFilePath))
  112. {
  113. strFilePath = gstrFileWebRoot.Trim('/') + "/" + strFilePath.Replace("\\", "/").Substring(strFilePath.ToLower().IndexOf("dmimg"));
  114. }
  115. //Add by Ethan, 2014/10/15 End. 處理圖片路徑變更
  116. string code = "出現錯誤";//MakeCode(5);
  117. MemoryStream ms = CreateCodeImg(code);
  118. if (!string.IsNullOrEmpty(strFilePath))
  119. {
  120. ms = new MemoryStream(File.ReadAllBytes(strFilePath));
  121. }
  122. if (null != ms)
  123. {
  124. context.Response.ClearContent();
  125. context.Response.ContentType = "image/Gif";
  126. context.Response.BinaryWrite(ms.ToArray());
  127. }
  128. }
  129. catch (Exception)
  130. {
  131. }
  132. }
  133. public bool IsReusable
  134. {
  135. get
  136. {
  137. return false;
  138. }
  139. }
  140. }
  141. }