ASP.NET 验证码控件

  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);
        }
    }

 

posted @ 2014-01-27 17:01  aswater  阅读(971)  评论(0编辑  收藏  举报