您的位置:首页 > 编程语言 > ASP

ASP.NET软件开发_ 旋转的可视验证码图象_源码

2011-09-13 15:50 369 查看
 
   /// <summary>

    /// 旋转的可视验证码图象

    /// </summary>

    public class VlidationCode

    {

        public enum RandomStringMode

        {

            /// <summary>

            /// 小写字母

            /// </summary>

            LowerLetter,

            /// <summary>

            /// 大写字母

            /// </summary>

            UpperLetter,

            /// <summary>

            /// 混合大小写字母

            /// </summary>

            Letter,

            /// <summary>

            /// 数字

            /// </summary>

            Digital,

            /// <summary>

            /// 混合数字与大小字母

            /// </summary>

            Mix

        }

        public static string GenerateRandomString(int length, RandomStringMode mode)

        {

            string rndStr = string.Empty;

            if (length == 0)

                return rndStr;

            //以数组方式候选字符,可以更方便的剔除不要的字符,如数字 0 与字母 o

            char[] digitals = new char[10] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

            char[] lowerLetters = new char[26] {

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

            char[] upperLetters = new char[26] {

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

            char[] letters = new char[52]{

                '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',

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

            char[] mix = new char[62]{

                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

                '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',

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

            int[] range = new int[2] { 0, 0 };

            Random random = new Random();

            switch (mode)

            {

                case RandomStringMode.Digital:

                    for (int i = 0; i < length; ++i)

                        rndStr += digitals[random.Next(0, digitals.Length)];

                    break;

                case RandomStringMode.LowerLetter:

                    for (int i = 0; i < length; ++i)

                        rndStr += lowerLetters[random.Next(0, lowerLetters.Length)];

                    break;

                case RandomStringMode.UpperLetter:

                    for (int i = 0; i < length; ++i)

                        rndStr += upperLetters[random.Next(0, upperLetters.Length)];

                    break;

                case RandomStringMode.Letter:

                    for (int i = 0; i < length; ++i)

                        rndStr += letters[random.Next(0, letters.Length)];

                    break;

                default:

                    for (int i = 0; i < length; ++i)

                        rndStr += mix[random.Next(0, mix.Length)];

                    break;

            }

            return rndStr;

        }

        public static void ShowCode(int i)

        {

             ShowValidationCode(ref i, 4, 20, RandomStringMode.UpperLetter, Color.Gold, Color.WhiteSmoke);

        }

        /// <summary>

        /// 显示验证码

        /// </summary>

        /// <param name="seed">随机数辅助种子</param>

        /// <param name="strLen">验证码字符长度</param>

        /// <param name="fontSize">字体大小</param>

        /// <param name="mode">随机字符模式</param>

        /// <param name="clrFont">字体颜色</param>

        /// <param name="clrBg">背景颜色</param>

        public static void ShowValidationCode(ref int seed, int strLen, int fontSize, RandomStringMode mode, Color clrFont, Color clrBg)

        {

            int tmpSeed;

            unchecked

            {

                tmpSeed = (int)(seed * DateTime.Now.Ticks);

                ++seed;

            }

            Random rnd = new Random(tmpSeed);

            string text = GenerateRandomString(strLen, mode);

            int height = fontSize * 2;

            // 因为字体旋转后每个字体所占宽度会所有加大,所以要加一点补偿宽度

            int width = fontSize * text.Length + fontSize / (text.Length - 2);

            Bitmap bmp = new Bitmap(width, height);

            Graphics graphics = Graphics.FromImage(bmp);

            Font font = new Font("Courier New", fontSize, FontStyle.Bold);

            Brush brush = new SolidBrush(clrFont);

            Brush brushBg = new SolidBrush(clrBg);

            graphics.FillRectangle(brushBg, 0, 0, width, height);

            Bitmap tmpBmp = new Bitmap(height, height);

            Graphics tmpGph = null;

            int degree = 40;

            Point tmpPoint = new Point();

            for (int i = 0; i < text.Length; i++)

            {

                tmpBmp = new Bitmap(height, height);

                tmpGph = Graphics.FromImage(tmpBmp);

               // tmpGph.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;

               // 不填充底色,文字 ClearType 效果不见了,why?!

               // tmpGph.FillRectangle(brushBg, 0, 0, tmpBmp.Width, tmpBmp.Height);

                degree = rnd.Next(20, 51); // [20, 50]随机角度

                if (rnd.Next(0, 2) == 0)

                {

                    tmpPoint.X = 12; // 调整文本坐标以适应旋转后的图象

                    tmpPoint.Y = -6;

                }

                else

                {

                    degree = ~degree + 1; // 逆时针旋转

                    tmpPoint.X = -10;

                    tmpPoint.Y = 6;

                }

                tmpGph.RotateTransform(degree);

                tmpGph.DrawString(text[i].ToString(), font, brush, tmpPoint);

                graphics.DrawImage(tmpBmp, i * fontSize, 0); // 拼接图象

            }

           

            //输出图象

            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();

            bmp.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Gif);

            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            HttpContext.Current.Response.ClearContent();

            HttpContext.Current.Response.ContentType = "image/gif";

            HttpContext.Current.Response.BinaryWrite(memoryStream.ToArray());

            HttpContext.Current.Response.End();

            //释放资源

            font.Dispose();

            brush.Dispose();

            brushBg.Dispose();

            tmpGph.Dispose();

            tmpBmp.Dispose();

            graphics.Dispose();

            bmp.Dispose();

            memoryStream.Dispose();

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息