.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能(详细版,可能会出现的问题的解决方法)

博客 James 1个月前 (05-28) 29次浏览 已收录 0个评论 扫描二维码

1.NuGet引入包 ZKWeb.System.Drawing,不会的自己Google。

.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能(详细版,可能会出现的问题的解决方法)

2、添加cs

.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能(详细版,可能会出现的问题的解决方法)

cs代码如下:

using System;
using System.DrawingCore;
using System.DrawingCore.Drawing2D;
using System.DrawingCore.Imaging;
using System.IO;

namespace XXXX.Common.Core.Helper
{
    public sealed class VerifyCodeHelper
    {
        #region 单例模式  
        //创建私有化静态obj锁    
        private static readonly object _ObjLock = new object();
        //创建私有静态字段,接收类的实例化对象    
        private static VerifyCodeHelper _VerifyCodeHelper = null;
        //构造函数私有化    
        private VerifyCodeHelper() { }
        //创建单利对象资源并返回    
        public static VerifyCodeHelper GetSingleObj()
        {
            if (_VerifyCodeHelper == null)
            {
                lock (_ObjLock)
                {
                    if (_VerifyCodeHelper == null)
                        _VerifyCodeHelper = new VerifyCodeHelper();
                }
            }
            return _VerifyCodeHelper;
        }
        #endregion

        #region 生产验证码  
        public enum VerifyCodeType { NumberVerifyCode, AbcVerifyCode, MixVerifyCode };

        /// <summary>  
        /// 1.数字验证码  
        /// </summary>  
        /// <param name="length"></param>  
        /// <returns></returns>  
        private string CreateNumberVerifyCode(int length)
        {
            int[] randMembers = new int[length];
            int[] validateNums = new int[length];
            string validateNumberStr = "";
            //生成起始序列值    
            int seekSeek = unchecked((int)DateTime.Now.Ticks);
            Random seekRand = new Random(seekSeek);
            int beginSeek = seekRand.Next(0, Int32.MaxValue - length * 10000);
            int[] seeks = new int[length];
            for (int i = 0; i < length; i++)
            {
                beginSeek += 10000;
                seeks[i] = beginSeek;
            }
            //生成随机数字    
            for (int i = 0; i < length; i++)
            {
                Random rand = new Random(seeks[i]);
                int pownum = 1 * (int)Math.Pow(10, length);
                randMembers[i] = rand.Next(pownum, Int32.MaxValue);
            }
            //抽取随机数字    
            for (int i = 0; i < length; i++)
            {
                string numStr = randMembers[i].ToString();
                int numLength = numStr.Length;
                Random rand = new Random();
                int numPosition = rand.Next(0, numLength - 1);
                validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
            }
            //生成验证码    
            for (int i = 0; i < length; i++)
            {
                validateNumberStr += validateNums[i].ToString();
            }
            return validateNumberStr;
        }

        /// <summary>  
        /// 2.字母验证码  
        /// </summary>  
        /// <param name="length">字符长度</param>  
        /// <returns>验证码字符</returns>  
        private string CreateAbcVerifyCode(int length)
        {
            char[] verification = new char[length];
            char[] dictionary = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
            };
            Random random = new Random();
            for (int i = 0; i < length; i++)
            {
                verification[i] = dictionary[random.Next(dictionary.Length - 1)];
            }
            return new string(verification);
        }

        /// <summary>  
        /// 3.混合验证码  
        /// </summary>  
        /// <param name="length">字符长度</param>  
        /// <returns>验证码字符</returns>  
        private string CreateMixVerifyCode(int length)
        {
            char[] verification = new char[length];
            char[] dictionary = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
            };
            Random random = new Random();
            for (int i = 0; i < length; i++)
            {
                verification[i] = dictionary[random.Next(dictionary.Length - 1)];
            }
            return new string(verification);
        }

        /// <summary>  
        /// 产生验证码(随机产生4-6位)  
        /// </summary>  
        /// <param name="type">验证码类型:数字,字符,符合</param>  
        /// <returns></returns>  
        public string CreateVerifyCode(VerifyCodeType type)
        {
            string verifyCode = string.Empty;
            Random random = new Random();
            int length = 4;// random.Next(4, 6);
            switch (type)
            {
                case VerifyCodeType.NumberVerifyCode:
                    verifyCode = GetSingleObj().CreateNumberVerifyCode(length);
                    break;
                case VerifyCodeType.AbcVerifyCode:
                    verifyCode = GetSingleObj().CreateAbcVerifyCode(length);
                    break;
                case VerifyCodeType.MixVerifyCode:
                    verifyCode = GetSingleObj().CreateMixVerifyCode(length);
                    break;
            }
            return verifyCode;
        }
        #endregion

        #region 验证码图片  
        /// <summary>  
        /// 验证码图片 => Bitmap  
        /// </summary>  
        /// <param name="verifyCode">验证码</param>  
        /// <param name="width"></param>  
        /// <param name="height"></param>  
        /// <returns>Bitmap</returns>  
        public Bitmap CreateBitmapByImgVerifyCode(string verifyCode, int width, int height)
        {
            Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic));
            Brush brush;
            Bitmap bitmap = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bitmap);
            SizeF totalSizeF = g.MeasureString(verifyCode, font);
            SizeF curCharSizeF;
            PointF startPointF = new PointF(0, (height - totalSizeF.Height) / 2);
            Random random = new Random(); //随机数产生器  
            g.Clear(Color.White); //清空图片背景色    
            for (int i = 0; i < verifyCode.Length; i++)
            {
                brush = new LinearGradientBrush(new Point(0, 0), new Point(1, 1), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)));
                g.DrawString(verifyCode[i].ToString(), font, brush, startPointF);
                curCharSizeF = g.MeasureString(verifyCode[i].ToString(), font);
                startPointF.X += curCharSizeF.Width;
            }

            //画图片的干扰线    
            for (int i = 0; i < 10; i++)
            {
                int x1 = random.Next(bitmap.Width);
                int x2 = random.Next(bitmap.Width);
                int y1 = random.Next(bitmap.Height);
                int y2 = random.Next(bitmap.Height);
                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }

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

            g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1); //画图片的边框线    
            g.Dispose();
            return bitmap;
        }

        /// <summary>  
        /// 验证码图片 => byte[]  
        /// </summary>  
        /// <param name="verifyCode">验证码</param>  
        /// <param name="width"></param>  
        /// <param name="height"></param>  
        /// <returns>byte[]</returns>  
        public byte[] CreateByteByImgVerifyCode(string verifyCode, int width, int height)
        {
            Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic));
            Brush brush;
            Bitmap bitmap = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bitmap);
            SizeF totalSizeF = g.MeasureString(verifyCode, font);
            SizeF curCharSizeF;
            PointF startPointF = new PointF(0, (height - totalSizeF.Height) / 2);
            Random random = new Random(); //随机数产生器  
            g.Clear(Color.White); //清空图片背景色    
            for (int i = 0; i < verifyCode.Length; i++)
            {
                brush = new LinearGradientBrush(new Point(0, 0), new Point(1, 1), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)));
                g.DrawString(verifyCode[i].ToString(), font, brush, startPointF);
                curCharSizeF = g.MeasureString(verifyCode[i].ToString(), font);
                startPointF.X += curCharSizeF.Width;
            }

            //画图片的干扰线    
            for (int i = 0; i < 10; i++)
            {
                int x1 = random.Next(bitmap.Width);
                int x2 = random.Next(bitmap.Width);
                int y1 = random.Next(bitmap.Height);
                int y2 = random.Next(bitmap.Height);
                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }

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

            g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1); //画图片的边框线    
            g.Dispose();

            //保存图片数据    
            MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, ImageFormat.Jpeg);
            //输出图片流    
            return stream.ToArray();

        }
        #endregion
    }
}

3、添加Controller

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System.DrawingCore.Imaging;
using System.IO;
using WoDian.Common.Core.Helper;

namespace WoDian.MVCBackstage.Core.Controllers
{
    public class VerifyCodeController : Controller
    {
        private IMemoryCache _cache;
        public VerifyCodeController(IMemoryCache memoryCache)
        {
            _cache = memoryCache;
        }
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>  
        /// 数字验证码  
        /// </summary>  
        /// <returns></returns>  
        public FileContentResult NumberVerifyCode()
        {
            string code = VerifyCodeHelper.GetSingleObj().CreateVerifyCode(VerifyCodeHelper.VerifyCodeType.NumberVerifyCode);
            _cache.Set("yzm", code);
            byte[] codeImage = VerifyCodeHelper.GetSingleObj().CreateByteByImgVerifyCode(code, 80, 40);
            return File(codeImage, @"image/jpeg");
        }

        /// <summary>  
        /// 字母验证码  
        /// </summary>  
        /// <returns></returns>  
        public FileContentResult AbcVerifyCode()
        {
            string code = VerifyCodeHelper.GetSingleObj().CreateVerifyCode(VerifyCodeHelper.VerifyCodeType.AbcVerifyCode);
            var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 100, 40);
            MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, ImageFormat.Png);
            return File(stream.ToArray(), "image/png");
        }

        /// <summary>  
        /// 混合验证码  
        /// </summary>  
        /// <returns></returns>  
        public FileContentResult MixVerifyCode()
        {
            string code = VerifyCodeHelper.GetSingleObj().CreateVerifyCode(VerifyCodeHelper.VerifyCodeType.MixVerifyCode);
            var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 100, 40);
            MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, ImageFormat.Gif);
            return File(stream.ToArray(), "image/gif");
        }
    }
}

4、页面中添加调用

.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能(详细版,可能会出现的问题的解决方法)

.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能(详细版,可能会出现的问题的解决方法)

<div class="col-md-12">
                <div class="form-group size-4">                   
                    <input class="required form-control "  name="CheckCode" placeholder="Code *" type="text" value="" style="width:70%">
                    <img id="mixImg" title="Code" src="/Validation/NumberVerifyCode" alt="vcode" onclick="refresh()" style="cursor:pointer;" />             
                </div>
            </div>

function refresh() {
    var id = document.getElementById("mixImg");
    var str = "/Validation/NumberVerifyCode?random=" + Math.random();
    id.setAttribute("src", str);
}

本地Windows 开发测试都是OK了

.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能(详细版,可能会出现的问题的解决方法)

/*******************************注意***********************************/

在部署到线上(针对线上是Linux服务器的可能会出现以下几种情况,大家注意了)

1.验证码图片显示不出来

那么按如下方式执行相关命令:

yum install autoconf automake libtool
yum install freetype-devel fontconfig libXft-devel
yum install libjpeg-turbo-devel libpng-devel giflib-devel libtiff-devel libexif-devel
yum install glib2-devel cairo-devel
git clone https://github.com/mono/libgdiplus
cd libgdiplus
./autogen.sh
make
make install
cd /usr/lib64/
ln -s /usr/local/lib/libgdiplus.so gdiplus.dll

运气好可能就正常,如果这些命令执行完还不行,那就要看是不是出现下面的问题:

2.验证码乱码的解决

这个主要是针对可以生成但是会出现乱码问题,说明是字体问题:

主要是解决字体问题。

在生成验证码的代码中, 关于字体的有一行:

string[] font = { "Times New Roman" };

所以先在  Windows 系统中找到:

复制到 Linux 中的 /usr/share/fonts/micro/ 下

mkdir /usr/share/fonts/micro/
cd /usr/share/fonts/micro/
# 字体文件复制到这个文件夹, 略
fc-cache  #重新加载字体
再重新执行 dotnet Web.dll , 验证码正常了
如果到这里正常了那么运气也是相当的好,可是不是所有人都有这么好运气,可能还会出现下面这种情况:

3.The type initializer for ‘System.DrawingCore.GDIPlus’ threw an exception

解决办法:

centos上安装libgdiplus-devel;

命令如下:yum install libgdiplus-devel 安装完成后即可解决上面问题

好了,大功告成,以上都是我亲身测试出来的,网上的教程都是零零碎碎,我这边就针对会遇到的多种情况都总结一下,希望对大家有帮助吧。如果还有其他情况可以评论私聊我,一起加油,奥力给。


老余博客, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能(详细版,可能会出现的问题的解决方法)
喜欢 (0)
[]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址