RSS订阅悠然探索,悠然索取
你的位置:首页 » 学习收藏 » 正文

asp.net图片验证码

选择字号: 超大 标准 发布时间:2011-1-11 14:31:16 | 作者:admin | 0个评论 | 人浏览

using System;
using System.Drawing;
using System.Web.UI.WebControls;

namespace Lc.Framework.Webadmin.Sysadmin
{
    public partial class code : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string checkCode = CreateCode(4);

            //用于验证
            Session["CheckCode"] = checkCode;
            CreateImages(checkCode);
        }

        /*产生验证码*/
        public string CreateCode(int codeLength)
        {

            string so = "1,2,3,4,5,6,7,8,9,0";
            string[] strArr = so.Split(',');
            string code = "";
            Random rand = new Random();
            for (int i = 0; i < codeLength; i++)
            {
                code += strArr[rand.Next(0, strArr.Length)];
            }
            return code;
        }

        /*产生验证图片*/
        public void CreateImages(string code)
        {

            Bitmap image = new Bitmap(60, 20);
            Graphics g = Graphics.FromImage(image);
            WebColorConverter ww = new WebColorConverter();
            g.Clear((Color)ww.ConvertFromString("#ffffff"));

            Random random = new Random();
            //画图片的背景噪音线
            for (int i = 0; i < 10; i++)
            {
                int x1 = random.Next(100);
                int x2 = random.Next(100);
                int y1 = random.Next(40);
                int y2 = random.Next(40);

                g.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2);
            }
            Font font = new Font("Arial", 12, FontStyle.Bold | FontStyle.Italic);
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
            new Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.Gray, 1.2f, true);

            //画验证码字符串
            for (int i = 0; i < code.Length; i++)
            {
                g.DrawString(code[i].ToString(), font, brush, i * 14 + 2, 2);
            }

            //画图片的前景噪音点
            //for (int i = 0; i < 300; i++)
            //{
            //    int x = random.Next(image.Width);
            //    int y = random.Next(image.Height);
            //    image.SetPixel(x, y, Color.LightGray);
            //}

            //画图片的边框线
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

            Response.Buffer = true;
            Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            Response.AppendHeader("Pragma", "No-Cache");
            //将验证码图片写入内存流,并将其以 "image/Png" 格式输出
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            try
            {
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                Response.ClearContent();
                Response.ContentType = "image/Png";
                Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                //显式释放资源
                g.Dispose();
                image.Dispose();

            }
        }
    }
}

标签:asp.net图片验证码  

猜你喜欢

发表评论

必填

选填

选填

必填,不填不让过哦,嘻嘻。

记住我,下次回复时不用重新输入个人信息

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。