.net 生成二维码图片

引用nuget包

QRCoder-ImageSharp

csharp 复制代码
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns>Bitmap对象</returns>
        public Image GenerateQRCode(string text, int width, int height)
        {
            using QRCodeGenerator qrGenerator = new QRCodeGenerator();
            using QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q);
            using QRCoder.BitmapByteQRCode bitmapByteQRCode = new BitmapByteQRCode(qrCodeData);
          

            var datas = bitmapByteQRCode.GetGraphic(5, "#000000", "#ffffff");
            Image image = Image.Load(datas);
            image.Mutate(ctx =>
            {
                ctx.Resize(width, height);
            });
            return image;
        }

        /// <summary>
        /// 生成带Logo的二维码
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <param name="logoImage">Logo</param>
        /// <returns>Bitmap对象</returns>
        public Image GenerateQRCodeWithLogo(string text, int width, int height, Image logoImage)
        {
            var qrCodeImage = GenerateQRCode(text, width, height);
            // 计算插入 logo 的位置


            int x = (width - logoImage.Width) / 2;
            int y = (height - logoImage.Width) / 2;

            qrCodeImage.Mutate(ctx => {
                ctx.DrawImage(logoImage, new Point(x, y), 1);
            });

            return qrCodeImage;
        }
相关推荐
深蓝海拓21 小时前
使matplot显示支持中文和负号
开发语言·python
syt_biancheng1 天前
Day3算法训练(简写单词,dd爱框框,3-除2!)
开发语言·c++·算法·贪心算法
864记忆1 天前
Qt Network 模块中的函数详解
开发语言·网络·qt
864记忆1 天前
Qt Sql 模块中的函数详解
开发语言·网络·qt
是店小二呀1 天前
五分钟理解Rust的核心概念:所有权Rust
开发语言·后端·rust
她说人狗殊途1 天前
存储引擎MySQL
开发语言
自然数e1 天前
C++多线程【线程管控】之线程转移以及线程数量和ID
开发语言·c++·算法·多线程
Arva .1 天前
ConcurrentHashMap 的线程安全实现
java·开发语言
Dxy12393102161 天前
Python为什么要使用可迭代对象
开发语言·python
任子菲阳1 天前
学Java第四十五天——斗地主小游戏创作
java·开发语言·windows