using System; using System.Collections.Generic; using System.Configuration; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using System.Linq; using System.Web; using DBUtility; using OT_Web.Ap_Code; using OT.Web.Ap_Code; namespace OT_Web { /// ///Common 的摘要描述 /// public class GetImg : IHttpHandler { //Add by Ethan, 2014/10/15 Start. 處理圖片路徑變更 private static string gstrFileWebRoot = ""; static GetImg() { gstrFileWebRoot = (DbHelperSQL.GetSingle(" SELECT SettingValue FROM dbo.OTB_SYS_SystemSetting WHERE SettingItem='DmImgWebPath' ") ?? "").ToString(); } //Add by Ethan, 2014/10/15 End. 處理圖片路徑變更 /// /// 生成驗證碼字符串 /// /// 驗證碼字符長度 /// 返回驗證碼字符串 private string MakeCode(int codeLen) { if (codeLen < 1) { return string.Empty; } int number; string checkCode = string.Empty; Random random = new Random(); for (int index = 0; index < codeLen; index++) { number = random.Next(); if (number % 2 == 0) { checkCode += (char)('0' + (char)(number % 10)); //生成數字 } else { checkCode += (char)('A' + (char)(number % 26)); //生成字母 } } return checkCode; } // /// /// 獲取驗證碼圖片流 /// /// 驗證碼字符串 /// 返回驗證碼圖片流 private MemoryStream CreateCodeImg(string checkCode) { if (string.IsNullOrEmpty(checkCode)) { return null; } Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); Graphics graphic = Graphics.FromImage(image); try { Random random = new Random(); graphic.Clear(Color.White); int x1 = 0, y1 = 0, x2 = 0, y2 = 0; for (int index = 0; index < 25; index++) { x1 = random.Next(image.Width); x2 = random.Next(image.Width); y1 = random.Next(image.Height); y2 = random.Next(image.Height); graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); } Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic)); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true); graphic.DrawString(checkCode, font, brush, 2, 2); int x = 0; int y = 0; //畫圖片的前景噪音點 for (int i = 0; i < 100; i++) { x = random.Next(image.Width); y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } //畫圖片的邊框線 graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); //將圖片驗證碼保存為流Stream返回 System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); return ms; } finally { graphic.Dispose(); image.Dispose(); } } public void ProcessRequest(HttpContext context) { try { string strFilePath = SecurityUtil.DESDecrypt(context.Request["F"].Replace(" ", "+"), "1234567890123456", "1234567890123456"); //Add by Ethan, 2014/10/15 Start. 處理圖片路徑變更 if (strFilePath != null && !File.Exists(strFilePath)) { strFilePath = gstrFileWebRoot.Trim('/') + "/" + strFilePath.Replace("\\", "/").Substring(strFilePath.ToLower().IndexOf("dmimg")); } //Add by Ethan, 2014/10/15 End. 處理圖片路徑變更 string code = "出現錯誤";//MakeCode(5); MemoryStream ms = CreateCodeImg(code); if (!string.IsNullOrEmpty(strFilePath)) { ms = new MemoryStream(File.ReadAllBytes(strFilePath)); } if (null != ms) { context.Response.ClearContent(); context.Response.ContentType = "image/Gif"; context.Response.BinaryWrite(ms.ToArray()); } } catch (Exception) { } } public bool IsReusable { get { return false; } } } }