예제 #1
0
        /// <summary>
        /// 产生全数字的验证码
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string VCodeNumber(int length)
        {
            string result = string.Empty;

            for (int i = 0; i < length; i++)
            {
                result += VerificationCode.GenerateRandomInteger(0, 10);
            }
            return(result);
        }
예제 #2
0
        /// <summary>
        /// 根据字符串产生验证码
        /// </summary>
        /// <param name="_Text"></param>
        /// <returns></returns>
        public static System.Drawing.Bitmap CreateNewImage(string _Text)
        {
            int      int_ImageWidth = _Text.Length * letterWidth;
            Bitmap   image          = new Bitmap(int_ImageWidth, letterHeight);
            Graphics g = Graphics.FromImage(image);

            g.Clear(Color.White);

            for (int i = 0; i < 2; i++)
            {
                int x1 = VerificationCode.GenerateRandomInteger(0, image.Width - 1);
                int x2 = VerificationCode.GenerateRandomInteger(0, image.Width - 1);
                int y1 = VerificationCode.GenerateRandomInteger(0, image.Height - 1);
                int y2 = VerificationCode.GenerateRandomInteger(0, image.Height - 1);
                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }

            int _x = -12, _y = 0;

            for (int int_index = 0; int_index < _Text.Length; int_index++)
            {
                _x += VerificationCode.GenerateRandomInteger(12, 16);
                _y  = VerificationCode.GenerateRandomInteger(-2, 2);
                string str_char = _Text.Substring(int_index, 1);
                str_char = VerificationCode.GenerateRandomInteger(0, 1) == 1 ? str_char.ToLower() : str_char.ToUpper();
                Brush newBrush = new SolidBrush(GetRandomColor());
                Point thePos   = new Point(_x, _y);
                g.DrawString(str_char, fonts[VerificationCode.GenerateRandomInteger(0, fonts.Length - 1)], newBrush, thePos);
            }
            for (int i = 0; i < 10; i++)
            {
                int x = VerificationCode.GenerateRandomInteger(0, image.Width - 1);
                int y = VerificationCode.GenerateRandomInteger(0, image.Height - 1);
                image.SetPixel(x, y, Color.FromArgb(VerificationCode.GenerateRandomInteger(0, 255), VerificationCode.GenerateRandomInteger(0, 255), VerificationCode.GenerateRandomInteger(0, 255)));
            }
            image = TwistImage(image, true, VerificationCode.GenerateRandomInteger(1, 3), VerificationCode.GenerateRandomInteger(4, 6));
            g.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, int_ImageWidth - 1, (letterHeight - 1));


            return(image);
        }