代码改变世界

验证码帮助类

2013-05-07 16:55  Carl Xing  阅读(199)  评论(0编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Drawing;
using System.IO;

    /// <summary>
    /// 登陆验证码帮助类
    /// </summary>
    public class LogonValidateCode
    {
        /// <summary>
        /// 比较验证码
        /// </summary>
        /// <param name="inputText">输入的验证码</param>
        /// <returns>是否验证成功</returns>
        public static bool CompareCode(string inputText)
        {
            bool isMatch = false;
            if (HttpContext.Current.Request.Cookies["CheckCode"] == null)
            {
                HttpContext.Current.Response.Write("<script type='text/javascript'>alert('您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。')</script>");
                return isMatch;
            }
            if (String.Compare(HttpContext.Current.Request.Cookies["CheckCode"].Value, inputText, true) != 0)
            {
                return isMatch;
            }
            isMatch = true;
            return isMatch;
        }

        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        public static string GenerateCheckCode()
        {
            int number;
            char code;
            string checkCode = String.Empty;

            System.Random random = new Random();

            for (int i = 0; i < 5; i++)
            {
                number = random.Next();

                if (number % 2 == 0)
                    code = (char)('0' + (char)(number % 10));
                else
                    code = (char)('A' + (char)(number % 26));

                checkCode += code.ToString();
            }
            //生成的COOKIE
            HttpContext.Current.Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));

            return checkCode;
        }

        /// <summary>
        /// 生产验证码的图片
        /// </summary>
        /// <param name="checkCode">验证码</param>
        public static void CreateCheckCodeImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return;

            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
            Graphics g = Graphics.FromImage(image);

            DrawCheckCodeImage(image, g, checkCode);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

            //System.Drawing.Bitmap bitImg = new System.Drawing.Bitmap(ms);
            //bitImg.Save(HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ContentType = "image/Gif";
            HttpContext.Current.Response.BinaryWrite(ms.ToArray());

            g.Dispose();
            image.Dispose();
        }
        /// <summary>
        /// 绘制验证码图片
        /// </summary>
        /// <param name="image">Bitmap对象实例</param>
        /// <param name="g">Graphics对象实例</param>
        /// <param name="checkCode">验证码</param>
        private static void DrawCheckCodeImage(Bitmap image, Graphics g, string checkCode)
        {
            //清空图片背景色
            g.Clear(Color.White);
            //生成随机生成器
            Random random = new Random();
            //画图片的背景噪音线
            for (int i = 0; i < 25; i++)
            {
                int x1 = random.Next(image.Width);
                int x2 = random.Next(image.Width);
                int y1 = random.Next(image.Height);
                int y2 = random.Next(image.Height);

                g.DrawLine(new Pen(Color.GreenYellow), x1, y1, x2, y2);
            }
            //画图片的验证码
            Font font = new System.Drawing.Font("Verdana", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
            g.DrawString(checkCode, font, brush, 2, 2);
            //画图片的前景噪音点
            for (int i = 0; i < 80; i++)
            {
                int x = random.Next(image.Width);
                int y = random.Next(image.Height);

                image.SetPixel(x, y, Color.FromArgb(random.Next()));
            }
            //画图片的边框线
            g.DrawRectangle(new Pen(Color.Red), 0, 0, image.Width - 1, image.Height - 1);

        }
    }