最新验证吗(可旋转)

/*********************************
 *
 * FileName:validate.cs
 * Copyright:(c) 2007-2-1
 * Writer:星星
 * Create Date:2007-2-1
 * Rewriter:
 * Rewrite Date:
 * Impact:
 * Main Content:验证码 可以启中文验证码  
 *
 *********************************/
using System.Web;
using System;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;


/// <summary>
///  验证码
/// </summary>
namespace ImageValidate
{
    /// <summary>
    /// 验证码
    /// </summary>
    /// <param name="m_nDisturbLine">干扰线</param>
    /// <param name="m_nDisturbPoint">干扰点</param>
    /// <param name="m_strFontSize">字体大小</param>
    /// <param name="m_strValidate">验证码字符串</param>
    public class Validate
    {
        #region 变量声明
        int         m_nDisturbLine = 0, m_nDisturbPoint = 0;        //干扰线,干扰点
        int         m_nImgWidth, m_nImgHeight;
        string      m_strValidate = null;                        //验证码字符串
        bool        m_bShowBorder; 
        Color       m_BorderColor, m_BgColor;
        Font        m_Font;                                     //字体
        string pp;
        #endregion

        #region 构造函数
        /// <summary>
        ///
        /// </summary>
        public Validate()
        {
            this.m_nImgWidth = 80;
            this.m_nImgHeight = 22;
            this. m_Font = new Font("宋体", 13, FontStyle.Bold);
            this.m_BorderColor = Color.Black;
            this.m_BgColor = Color.White;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="nDisturbLine">干扰线</param>
        /// <param name="nDisturbPoint">干扰点</param>
        /// <param name="bgColor">背景色</param>
        public Validate(int nDisturbLine, int nDisturbPoint, Color bgColor,bool bShowBorder)
        {
            this.m_nDisturbLine = nDisturbLine;
            this.m_nDisturbPoint = nDisturbPoint;
            this.m_BgColor = bgColor;

            this.m_bShowBorder = bShowBorder;
            this.m_Font = new Font("宋体", 13, FontStyle.Bold);
            this.m_BorderColor = Color.Black;
            this.m_BgColor = Color.White;
        }

        #endregion

        #region  属性
        /// <summary>
        /// 获取验证码字符串 用以保存在Session
        /// </summary>
        public string strValidate
        {
            get { return this.m_strValidate; }
        }

        /// <summary>
        /// 背景颜色
        /// </summary>
        public Color BgColor
        {
            set { this.m_BgColor = value; }
        }

         /// <summary>
        /// 是否显示边框
        /// </summary>
        public bool ShowBorder
        {
            set { this.m_bShowBorder = value; }
        }

         /// <summary>
        /// 边框Color
        /// </summary>
        public Color BorderColorr
        {
            set { this.m_BorderColor = value; }
        }

        /// <summary>
        /// 边框Color
        /// </summary>
        public Font ValidateFont
        {
            set { this.m_Font = value; }
        }
        #endregion

     
        /// <summary>
        /// 获取字母数字验证码字符串
        /// </summary>
        /// <param name="nLength">验证码字符个数</param>
        /// <returns>验证码字符串</returns>
        private string GetStrAscii(int nLength)
        {
            int     nStrLength  = nLength;
            string  strString   = "1234567890ASDFGHJKLMNBVCXZQWERTYUIOP";
            StringBuilder strtemp = new StringBuilder();  
            Random random = new Random((int)DateTime.Now.Ticks);
            for (int i = 0; i < nStrLength; i++)
            {
                random = new Random(unchecked(random.Next() * 1000));
                strtemp.Append(strString[random.Next(36)]);
            }
            return strtemp.ToString();
        }


        /// <summary>
        /// 获取中文验证码字符串
        /// </summary>
        /// <param name="nLength">验证码字符中文的个数</param>
        /// <returns>验证码字符串(中文)</returns>
        private string GetStrChinese(int nLength)
        {
            int      nStrLength = nLength;
            object[] btStrings  = new object[nStrLength];
            string[] strString  = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

            Random random = new Random();
            for (int i = 0; i < nStrLength; i++)
            {
                //区位码第1位  
                int     nNext1   = random.Next(11, 14);
                string  strChar1 = strString[nNext1];

                //区位码第2位  
                random = new Random(nNext1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的  

                int nNext2;
                if (nNext1 == 13)
                {
                    nNext2 = random.Next(0, 7);
                }
                else
                {
                    nNext2 = random.Next(0, 16);
                }
                string strChar2 = strString[nNext2];

                //区位码第3位  
                random  = new Random(nNext2 * unchecked((int)DateTime.Now.Ticks) + i);
                int     nNext3   = random.Next(10, 16);
                string  strChar3 = strString[nNext3];

                //区位码第4位  
                random  = new Random(nNext3 * unchecked((int)DateTime.Now.Ticks) + i);
                int nNext4;
                if (nNext3 == 10)
                {
                    nNext4 = random.Next(1, 16);
                }
                else if (nNext3 == 15)
                {
                    nNext4 = random.Next(0, 15);
                }
                else
                {
                    nNext4 = random.Next(0, 16);
                }
                string strChar4 = strString[nNext4];

                byte    bt1      = Convert.ToByte(strChar1 + strChar2, 16);
                byte    bt2      = Convert.ToByte(strChar3 + strChar4, 16);
                byte[]  btString = new byte[2] { bt1, bt2 };

                btStrings.SetValue(btString, i);
            }

            //转化成汉字
            StringBuilder   strReturn = new StringBuilder();
            Encoding        edGb2312 = Encoding.GetEncoding("gb2312");
            for (int i = 0; i < nStrLength; i++)
            {
                strReturn.Append(edGb2312.GetString((byte[])Convert.ChangeType(btStrings[i], typeof(byte[]))));
            }
            return strReturn.ToString();
        }

        /// <summary>
        /// 获取验证码图片
        /// </summary>
        /// <param name="nValidateLength">验证码字符个数</param>
        /// <param name="nImgWidth">图片宽度</param>
        /// <param name="nImgHeight">图片高度</param>
        /// <param name="p_CharacterSpacing">字间</param>
        /// <returns>流的图片</returns>
        public MemoryStream GetCode(bool p_bValidateType, int nValidateLength, int nImgWidth, int nImgHeight ,int nSpace)
        {
            MemoryStream    stream = null;
            Bitmap image = new Bitmap(nImgWidth, nImgHeight);
            Graphics grImg = Graphics.FromImage(image);

            if (p_bValidateType)
            {   //汉字验证码
                m_strValidate = this.GetStrChinese(nValidateLength);
            }
            else
            {   //字母数字验证码
                m_strValidate = this.GetStrAscii(nValidateLength);
            }

            try
            {
                Random  random = new Random();
                grImg.Clear(m_BgColor);    //清空图片背景色
                for (int i = 0; i < m_nDisturbLine; 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);
                    grImg.DrawLine(new Pen(Color.FromArgb(random.Next(150), random.Next(255), random.Next(100)),1), x1, y1, x2, y2);
                }

                Bitmap imgTemp, RotateTransImg;
                Graphics grh;
                LinearGradientBrush brush;
                GraphicsPath path;
                Matrix mtrx;
                RectangleF rectanglef;
                int angle, height = (int)m_Font.GetHeight(grImg)+3,width = height;

                //逐个制在背景图片上
                for (int i = 0; i < m_strValidate.Length; i++)
                {
                    random = new Random( unchecked((int)DateTime.Now.Ticks) + i);       //更换随机数发生器的  
                    //brush = new SolidBrush(Color.FromArgb(random.Next(100),random.Next(50),random.Next(50)));
                    Color color = Color.FromArgb(random.Next(100), random.Next(50), random.Next(50));
                    brush = new LinearGradientBrush(new Rectangle(0, 0, width, height), color, color, 0, false);
                   
                    //绘制单个图片
                    imgTemp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
                    grh = Graphics.FromImage(imgTemp);
                    grh.Clear(Color.FromArgb(0, 255, 255, 255));
                    grh.DrawString(m_strValidate[i].ToString(),m_Font, brush, 2, 2);
                    grh.Dispose();

                    angle = random.Next(-15, 15); //旋转角度

                    //生成旋转的图片大小
                    path = new GraphicsPath();
                    path.AddRectangle(new RectangleF(0f, 0f, width, height));
                    mtrx = new Matrix();
                    mtrx.Rotate(angle);
                    rectanglef = path.GetBounds(mtrx);
                    RotateTransImg = new Bitmap((int)rectanglef.Width, (int)rectanglef.Height, PixelFormat.Format32bppArgb);

                    //生成旋转图片
                    grh = Graphics.FromImage(RotateTransImg);
                    grh.Clear(Color.FromArgb(0, 255, 255, 255));
                    //grh.TranslateTransform(-rectanglef.X, -rectanglef.Y);
                    grh.RotateTransform(angle);
                    grh.DrawImageUnscaled(imgTemp, 1, 1);

                    grImg.DrawImage(RotateTransImg, new Point(4 + i * nSpace, 5 + random.Next(-4, 4)));    //绘在背景图片上

                    imgTemp.Dispose();
                    RotateTransImg.Dispose();
                    grh.Dispose();
                    path.Dispose();
                }
             

                for (int i = 0; i < m_nDisturbPoint; i++)
                {   //画图片的前景干扰点
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next(150), random.Next(255), random.Next(100)));
                }

                if (m_bShowBorder)
                {   //画图片的边框线
                    grImg.DrawRectangle(new Pen(m_BorderColor), 0, 0, image.Width - 1, image.Height - 1);
                }

                //保存图片数据
                stream = new MemoryStream();
                image.Save(stream, ImageFormat.Jpeg);
            }
            finally
            {
                grImg.Dispose();
                image.Dispose();
            }
            return stream;
        }
    }
}
 

你可能感兴趣的:(String,Stream,Random,byte,Matrix,BT)