.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;
        }
相关推荐
Highcharts.js1 天前
教程:基于 React + Highcharts 构建一个单页应用程序、按需数据拉取与图表渲染
开发语言·前端·数据结构·react.js·前端框架·highcharts·页面应用
头发还在的女程序员1 天前
医院陪诊管理系统怎么选择?——2026 年选型避坑与架构参考
java·开发语言·陪诊系统·陪诊app·医院陪诊陪护
爱写代码的小朋友1 天前
从零开始学 Win32 API:C++ 窗口编程实战(VS Code + MinGW-w64 命令行详解)
开发语言·c++
果汁华1 天前
Function Calling 与 Python 实战完整指南
开发语言·网络·python
小小晓.1 天前
C++:语句和作用域
开发语言·c++
wanderist.1 天前
Lambda表达式在算法竞赛中的应用
java·开发语言·算法
海天鹰1 天前
PHP上传文件
android·开发语言·php
Yeauty1 天前
渲染成图再 CLI 拼接,还是进程内直推?Rust 帧到视频的两条路
开发语言·rust·音视频
geovindu1 天前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
库克克1 天前
【C++】set 与multiset
开发语言·c++