获取图形验证码

       /// <summary>
        /// 获取图形验证码
        /// </summary>
        /// <param name="postVal"></param>
        /// <returns></returns>
        [HttpPost]
             
        public IActionResult   ValidateImgCode([FromBody]JObject postVal)
        {

            int width =  100;
            int height = 40;
            int fontsize = 20;
            string ImgCode = string.Empty;
            
            //第一种用内存流的返回值
            //
            System.IO.MemoryStream ms = ValidateCode.Create_Validate_Graphic(out ImgCode, 4, width, height, fontsize);
             return File(ms.ToArray(), @"image/jpeg");


            //
            //下面是用字节返回
            //
            //byte[] bytes =  ValidateCode.CreateValidateGraphic(out ImgCode, 4, width, height, fontsize);
            // return File(bytes, @"image/jpeg");
            //return AsResult.Success(new
            //{
            //    CodeImg = ImgCode,
            //    CodeByte= File(bytes, @"image/jpeg")
            //}
            //);
            //
            //

        }
 /// <summary>
    /// 随机码和图片流生成
    /// </summary>
    public class ValidateCode
    {

                /// <summary>
                /// 产生图像验证码
                /// </summary>
                /// <param name="Code">验证码。</param>
                /// <param name="CodeLength">验证码元数。</param>
                /// <param name="Width"></param>
                /// <param name="Height"></param>
                /// <param name="FontSize"></param>
                /// <returns></returns>       
        public static byte[] CreateValidateGraphic(out String ImgCode, int CodeLength, int Width, int Height, int FontSize)
        {
            String sCode = String.Empty;
            //顏色列表,用于验证码噪点 噪线
             Color[] oColors ={
             Color.Black,
             Color.Red,
             Color.Blue,
             Color.Green,
             Color.Orange,
             Color.Brown,
             Color.Brown,
             Color.DarkBlue};
            //字体列表用于验证码
            string[] oFontNames = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
            //驗證碼的字元集,去掉了一些容易混淆的字元
            char[] oCharacter = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
            Random oRnd = new Random();
            Bitmap oBmp = null;
            Graphics oGraphics = null;
            int N1 = 0;
            Point oPoint1 = default(Point);
            Point oPoint2 = default(Point);
            string sFontName = null;
            Font oFont = null;
            Color oColor = default(Color);
            //生成验证码字串
            for (N1 = 0; N1 <= CodeLength - 1; N1++)
            {
                sCode += oCharacter[oRnd.Next(oCharacter.Length)];
            }
            //定义图像的大小,生成图像的实例 
            oBmp = new Bitmap(Width, Height);
            //从Img对象生成新的Graphics对象
            oGraphics = Graphics.FromImage(oBmp);
            //背景设为白色
            oGraphics.Clear(Color.White);
            try
            {
                for (N1 = 0; N1 <= 4; N1++)
                {
                    //画噪线
                    oPoint1.X = oRnd.Next(Width);
                    oPoint1.Y = oRnd.Next(Height);
                    oPoint2.X = oRnd.Next(Width);
                    oPoint2.Y = oRnd.Next(Height);
                    oColor = oColors[oRnd.Next(oColors.Length)];
                    oGraphics.DrawLine(new Pen(oColor), oPoint1, oPoint2);
                }
                float spaceWith = 0, dotX = 0, dotY = 0;
                if (CodeLength != 0)
                {
                    spaceWith = (Width - FontSize * CodeLength - 10) / CodeLength;
                }
                for (N1 = 0; N1 <= sCode.Length - 1; N1++)
                {
                    //画验证码子串
                    sFontName = oFontNames[oRnd.Next(oFontNames.Length)];
                    oFont = new Font(sFontName, FontSize, FontStyle.Italic);
                    oColor = oColors[oRnd.Next(oColors.Length)];
                    dotY = (Height - oFont.Height) / 2 + 2;//中心下移2像素
                    dotX = Convert.ToSingle(N1) * FontSize + (N1 + 1) * spaceWith;
                    oGraphics.DrawString(sCode[N1].ToString(), oFont, new SolidBrush(oColor), dotX, dotY);
                }
                //在随机位置画背景点 
                for (int i = 0; i <= 30; i++)
                {
                    //画噪点
                    int x = oRnd.Next(oBmp.Width);
                    int y = oRnd.Next(oBmp.Height);
                    Color clr = oColors[oRnd.Next(oColors.Length)];
                    oBmp.SetPixel(x, y, clr);
                }
                ImgCode = sCode;
                //保存图片数据
                MemoryStream stream = new MemoryStream();
                oBmp.Save(stream, ImageFormat.Jpeg);
                //输出图片流
                return  stream.ToArray();
            }
            
            finally
            {
                oGraphics.Dispose();
                oBmp.Dispose();
            }

        }


        /// <summary>
        /// 内存流的方式返回  随机码和图片流生成
        /// </summary>
        /// <param name="ImgCode"></param>
        /// <param name="CodeLength"></param>
        /// <param name="Width"></param>
        /// <param name="Height"></param>
        /// <param name="FontSize"></param>
        /// <returns></returns>
        public static MemoryStream Create_Validate_Graphic(out String ImgCode, int CodeLength, int Width, int Height, int FontSize)
        {
            String sCode = String.Empty;
            //顏色列表,用于验证码噪点 噪线
            Color[] oColors ={
             Color.Black,
             Color.Red,
             Color.Blue,
             Color.Green,
             Color.Orange,
             Color.Brown,
             Color.Brown,
             Color.DarkBlue};
            //字体列表用于验证码
            string[] oFontNames = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
            //驗證碼的字元集,去掉了一些容易混淆的字元
            char[] oCharacter = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
            Random oRnd = new Random();
            Bitmap oBmp = null;
            Graphics oGraphics = null;
            int N1 = 0;
            Point oPoint1 = default(Point);
            Point oPoint2 = default(Point);
            string sFontName = null;
            Font oFont = null;
            Color oColor = default(Color);
            //生成验证码字串
            for (N1 = 0; N1 <= CodeLength - 1; N1++)
            {
                sCode += oCharacter[oRnd.Next(oCharacter.Length)];
            }
            //定义图像的大小,生成图像的实例 
            oBmp = new Bitmap(Width, Height);
            //从Img对象生成新的Graphics对象
            oGraphics = Graphics.FromImage(oBmp);
            //背景设为白色
            oGraphics.Clear(Color.White);
            try
            {
                for (N1 = 0; N1 <= 4; N1++)
                {
                    //画噪线
                    oPoint1.X = oRnd.Next(Width);
                    oPoint1.Y = oRnd.Next(Height);
                    oPoint2.X = oRnd.Next(Width);
                    oPoint2.Y = oRnd.Next(Height);
                    oColor = oColors[oRnd.Next(oColors.Length)];
                    oGraphics.DrawLine(new Pen(oColor), oPoint1, oPoint2);
                }
                float spaceWith = 0, dotX = 0, dotY = 0;
                if (CodeLength != 0)
                {
                    spaceWith = (Width - FontSize * CodeLength - 10) / CodeLength;
                }
                for (N1 = 0; N1 <= sCode.Length - 1; N1++)
                {
                    //画验证码子串
                    sFontName = oFontNames[oRnd.Next(oFontNames.Length)];
                    oFont = new Font(sFontName, FontSize, FontStyle.Italic);
                    oColor = oColors[oRnd.Next(oColors.Length)];
                    dotY = (Height - oFont.Height) / 2 + 2;//中心下移2像素
                    dotX = Convert.ToSingle(N1) * FontSize + (N1 + 1) * spaceWith;
                    oGraphics.DrawString(sCode[N1].ToString(), oFont, new SolidBrush(oColor), dotX, dotY);
                }
                //在随机位置画背景点 
                for (int i = 0; i <= 30; i++)
                {
                    //画噪点
                    int x = oRnd.Next(oBmp.Width);
                    int y = oRnd.Next(oBmp.Height);
                    Color clr = oColors[oRnd.Next(oColors.Length)];
                    oBmp.SetPixel(x, y, clr);
                }
                ImgCode = sCode;
                //保存图片数据
                MemoryStream stream = null;

             stream = new MemoryStream();
                oBmp.Save(stream, ImageFormat.Jpeg);
                //输出图片流
                return stream;
            }

            finally
            {
                oGraphics.Dispose();
                oBmp.Dispose();
            }

        }
    }

 

转载于:https://www.cnblogs.com/Warmsunshine/p/8615069.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值