public class ValidateCode : WebControl { /// <summary> /// 默認構造函數,暴露的屬性接口 /// </summary> public ValidateCode() { this.CharCount = 4; this.Width = 130; this.Height = 30; CodeColor = Color.White; } /// <summary> /// 設置參數 /// </summary> /// <param name="CharCount"></param> /// <param name="Width"></param> /// <param name="Height"></param> /// <param name="color"></param> public ValidateCode(int CharCount, int Width, int Height,Color color) { this.CharCount = CharCount; this.Width = Width; this.Height = Height; this.CodeColor = color; } /// <summary> /// 字符個數 /// </summary> public int CharCount { get; set; } /// <summary> /// 圖標矩形寬度 /// </summary> public new int Width { set; get; } /// <summary> /// 圖標矩形高度 /// </summary> public new int Height { set; get; } /// <summary> /// Code背景色 /// </summary> public Color CodeColor { get; set; } /// <summary> /// 驗證驗證碼是否正確 /// </summary> /// <param name="sn"></param> /// <returns></returns> public bool checkCode(string sn) { return (sn.ToUpper() == this.Page.Request.Cookies["validateCookie"].Values["ChkCode"].ToString().ToUpper());//讀取Cookie } protected override void OnInit(EventArgs e) { base.OnInit(e); if (this.DesignMode) return; string str = this.Page.Request.QueryString["_xuImageTag"]; if (str != "1") return; HttpResponse resp = this.Page.Response; string chkCode = string.Empty; //顏色列表,用於驗證碼、噪線、噪點 Color[] color = { Color.Red, Color.Black, Color.Blue, Color.Green, Color.Orange }; string[] font = { "宋體", "Times New Roman", "MS Mincho", "楷體", "隸書", "微軟雅黑","Calibri" }; char[] character ={'1','2','4','3','5','6','7','8','9','0','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'}; Random rnd = new Random(); for (int i = 0; i < this.CharCount; i++) { chkCode += character[rnd.Next(character.Length)]; } resp.Cookies["validateCookie"].Values["ChkCode"] =chkCode;//驗證碼寫入Cookie Bitmap bmp = new Bitmap(this.Width,this.Height); Graphics g = Graphics.FromImage(bmp); g.Clear(this.CodeColor); //畫噪線 for (int i = 0; i < 5; i++) { int x1 = rnd.Next(this.Width); int y1 = rnd.Next(this.Height); int x2 = rnd.Next(this.Width); int y2 = rnd.Next(this.Height); Color clr = color[rnd.Next(color.Length)]; g.DrawLine(new Pen(clr), x1, y1, x2, y2); } //畫驗證字符串 for (int i = 0; i < chkCode.Length; i++) { string fnt = font[rnd.Next(font.Length)]; Font ft = new Font(fnt, 12); Color clr = color[rnd.Next(color.Length)]; g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * this.Width/chkCode.Length+2+10, ((float)this.Height)/5); } for (int i = 0; i < (this.Width*this.Height)/64; i++) { int x = rnd.Next(bmp.Width); int y = rnd.Next(bmp.Height); Color clr = color[rnd.Next(color.Length)]; bmp.SetPixel(x, y, clr); } //將驗證圖片寫入內存流,並將以image/Png格式輸出 MemoryStream ms = new MemoryStream(); try { bmp.Save(ms, ImageFormat.Png); resp.ClearContent(); resp.ContentType = "image/Png"; resp.BinaryWrite(ms.ToArray()); resp.Flush(); resp.End(); } finally { bmp.Dispose(); g.Dispose(); } } protected override void Render(HtmlTextWriter writer) { if (!this.DesignMode) { writer.Write("<img border=\"0\" src=\"{0}\">", this.Page.Request.Path + "?_xuImageTag=1"); } else { writer.Write("驗證碼"); } base.Render(writer); } }