• C# 验证码生成(MVC和非MVC两种方式)


            /// <summary>
            /// 生成验证码
            /// </summary>
            /// <param name="length">指定验证码的长度</param>
            /// <returns></returns>
            public string CreateValidateCode(int length)
            {
                int rand;
                char code;
                string randomcode = String.Empty;
                //生成一定长度的验证码  
                System.Random random = new Random();
                for (int i = 0; i < length; i++)
                {
                    rand = random.Next();
                    if (rand % 3 == 0)
                    {
                        code = (char)('A' + (char)(rand % 26));
                    }
                    else
                    {
                        code = (char)('0' + (char)(rand % 10));
                    }
                    randomcode += code.ToString();
                }
                return randomcode;
            }
    
            /// <summary>
            /// 创建验证码的图片(非MVC模式下使用)
            /// </summary>
            /// <param name="containsPage">要输出到的page对象</param>
            /// <param name="validateCode">验证码</param>
            /// <param name="fontSize">字体大小</param>
            public void CreateValidateGraphic(HttpContext containsPage, string validateCode, int fontSize = 20)
            {
                Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 21.0), (int)Math.Ceiling(fontSize * 1.8));
                Graphics g = Graphics.FromImage(image);
                try
                {
                    //生成随机生成器
                    Random random = new Random();
                    //清空图片背景色
                    g.Clear(Color.White);
                    //画图片的干扰线
                    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.Silver), x1, y1, x2, y2);
                    }
                    Font font = new Font("Arial", fontSize, (FontStyle.Bold | FontStyle.Italic));
                    LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
                     Color.Blue, Color.DarkRed, 1.2f, true);
                    g.DrawString(validateCode, font, brush, 3, 2);
                    //画图片的前景干扰点
                    for (int i = 0; i < 100; 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.Silver), 0, 0, image.Width - 1, image.Height - 1);
                    //保存图片数据
                    MemoryStream stream = new MemoryStream();
                    image.Save(stream, ImageFormat.Jpeg);
                    //输出图片流
                    containsPage.Response.Clear();
                    containsPage.Response.ContentType = "image/jpeg";
                    containsPage.Response.BinaryWrite(stream.ToArray());
                }
                finally
                {
                    g.Dispose();
                    image.Dispose();
                }
            }
    
            /// <summary>
            /// 创建验证码的图片(MVC模式下使用)
            /// </summary>
            /// <param name="validateCode">验证码</param>
            /// <param name="fontSize">字体大小</param>
            public byte[] CreateValidateGraphic(string validateCode, int fontSize = 20)
            {
                Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 21.0), (int)Math.Ceiling(fontSize * 1.8));
                Graphics g = Graphics.FromImage(image);
                try
                {
                    //生成随机生成器
                    Random random = new Random();
                    //清空图片背景色
                    g.Clear(Color.White);
                    //画图片的干扰线
                    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.Silver), x1, y1, x2, y2);
                    }
                    Font font = new Font("Arial", fontSize, (FontStyle.Bold | FontStyle.Italic));
                    LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
                     Color.Blue, Color.DarkRed, 1.2f, true);
                    g.DrawString(validateCode, font, brush, 3, 2);
                    //画图片的前景干扰点
                    for (int i = 0; i < 100; 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.Silver), 0, 0, image.Width - 1, image.Height - 1);
                    //保存图片数据
                    MemoryStream stream = new MemoryStream();
                    image.Save(stream, ImageFormat.Jpeg);
                    //输出图片流
                    return stream.ToArray();
                }
                finally
                {
                    g.Dispose();
                    image.Dispose();
                }
            }
             /// <summary>
            /// 验证码Key
            /// </summary>
            public const string ValidateCodeKey = "ValidateCode";
    
          /// <summary>
            /// 获取验证码
            /// </summary>
            /// <returns></returns>
            [OutputCache(Duration = 0)]
            public ActionResult VerifyCode()
            {
                ValidateCode vCode = new ValidateCode();
                string code = vCode.CreateValidateCode(5);
                Session[ConstHelper.ValidateCodeKey] = code;
                byte[] bytes = vCode.CreateValidateGraphic(code);
                return File(bytes, @"image/jpeg");
            }
  • 相关阅读:
    php设计模式-装饰器模式
    php设计模式-观察者模式
    play-with-docker搭配ffsend完成文件上传及下载(解决从docker hub拉取镜像慢问题)
    java执行命令行,以及解决卡死问题
    在docker容器中调用docker命令
    MIME 类型,ContentType
    大文件下载---之内存溢出问题解决
    OperationCenter Docker运行环境及其依赖启动脚本
    快速获取批量处理Docker镜像SQL语句
    解决达梦数据库新增大字段报错问题
  • 原文地址:https://www.cnblogs.com/daxiongblog/p/4668500.html
Copyright © 2020-2023  润新知