• Asp.Net Core 生成图形验证码


    前几天有朋友问我怎么生成图片验证码,话不多说直接上代码。

    支持.NET CORE开源。助力.NET Core社区发展。

     1 using System;
     2 using System.IO;
     3 using System.DrawingCore.Imaging;
     4 using System.DrawingCore;
     5 using CZFW.Framework.Model;
     6 
     7 namespace CZFW.Core.Security
     8 {
     9     /// <summary>
    10     /// 图形验证码
    11     /// </summary>
    12     public class VerifyCode
    13     {
    14         public byte[] GetVerifyCode()
    15         {
    16             const int codeW = 80;
    17             const int codeH = 30;
    18             const int fontSize = 16;
    19             string chkCode = string.Empty;
    20             //颜色列表,用于验证码、噪线、噪点 
    21             Color[] color = { Color.Black,Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
    22             //字体列表,用于验证码 
    23             string[] font = { "Times New Roman" };
    24             //验证码的字符集,去掉了一些容易混淆的字符 
    25             char[] character = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
    26             Random rnd = new Random();
    27             //生成验证码字符串 
    28             for (int i = 0; i < 4; i++)
    29             {
    30                 chkCode += character[rnd.Next(character.Length)];
    31             }
    32             //写入Session、验证码加密
    33             WebHelper.WriteSession("czfw_session_verifycode", DesEncrypt.Encrypt(chkCode.ToLower(), "MD5"));
    34             //创建画布
    35             Bitmap bmp = new Bitmap(codeW, codeH);
    36             Graphics g = Graphics.FromImage(bmp);
    37             g.Clear(Color.White);
    38             //画噪线 
    39             for (int i = 0; i < 3; i++)
    40             {
    41                 int x1 = rnd.Next(codeW);
    42                 int y1 = rnd.Next(codeH);
    43                 int x2 = rnd.Next(codeW);
    44                 int y2 = rnd.Next(codeH);
    45                 Color clr = color[rnd.Next(color.Length)];
    46                 g.DrawLine(new Pen(clr), x1, y1, x2, y2);
    47             }
    48             //画验证码字符串 
    49             for (int i = 0; i < chkCode.Length; i++)
    50             {
    51                 string fnt = font[rnd.Next(font.Length)];
    52                 Font ft = new Font(fnt, fontSize);
    53                 Color clr = color[rnd.Next(color.Length)];
    54                 g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18, 0);
    55             }
    56             //将验证码图片写入内存流,并将其以 "image/Png" 格式输出 
    57             MemoryStream ms = new MemoryStream();
    58             try
    59             {
    60                 bmp.Save(ms, ImageFormat.Png);
    61                 return ms.ToArray();
    62             }
    63             catch (Exception)
    64             {
    65                 return null;
    66             }
    67             finally
    68             {
    69                 g.Dispose();
    70                 bmp.Dispose();
    71             }
    72         }
    73     }
    74 }

    上面的是生成图片下面是在控制器中使用

      public ActionResult GetAuthCode()
            {
                return File(new VerifyCode().GetVerifyCode(), @"image/Gif");
            }

    用File返回到web页面上就是一个验证码图片了。

  • 相关阅读:
    SpringData JPA @Query动态SQL语句,且分页
    Springboot项目修改html后不需要重启---springboot项目的热部署
    sqlserver如何进行日期转换
    导入项目时候出现 Description Resource Path Location Type Cannot change version of project facet Dynamic Web Module to 2.5错误
    Intellij IDEA和Eclipse之间的常用快捷键
    Java 集合转换(数组、List、Set、Map相互转换)
    oracle日期格式转换 to_date(),to_char()
    java数据缓存
    VMware14安装Centos8详细教程图解教程
    CentOS7 安装Xfce桌面环境
  • 原文地址:https://www.cnblogs.com/Tassdar/p/9706952.html
Copyright © 2020-2023  润新知