惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
H
Help Net Security
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
A
About on SuperTechFans
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
宝玉的分享
宝玉的分享
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
博客园 - 【当耐特】
Microsoft Security Blog
Microsoft Security Blog
Martin Fowler
Martin Fowler
I
InfoQ
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
U
Unit 42
The Cloudflare Blog
Y
Y Combinator Blog

博客园 - 牛腩

鸿蒙开发DevEco Studio创建hello world项目 SQL Server数据库中用存储过程来取顺序号 cmd命令行下用命令执行SQL脚本到SQL Server数据库中 Blazor项目中建立WebApi NETCORE下用SKIT类库发送微信模板消息 ASP.NET CORE微信支付回调示例代码 nuget离线安装 高德地图API使用(根据地址查经纬度,计算二个坐标之间的距离) ModernWMS - 仓库管理系统简记 .NET开源商城CoreShop简记 windows服务器上用nginx转发到iis中的网站 uniapp中防抖函数debounce的使用 winform中的picturebox控件显示和保存byte[]图片 ASP.NET CORE kindeditor在线编辑器示例(上传多图和插入VIDEO标签) uni app uview新增商品页(无限级分类选择和多图上传) C#获取HTML源码 Ant for Blazor做单个表的增删查改 ASP.NET MVC中使用Autofac依赖注入 NET 7 中使用Session
NET8下生成二维码
牛腩 · 2024-02-20 · via 博客园 - 牛腩

NET8下生成二维码

按网上搜索的总是多少有些问题,得总搜索好几次才能解决的,现把自己用过的可以生成的代码放上来,以备后用

2024年02月20日在 VS2022,NET8,MVC 项目上使用通过

引入NUGET:ZXing.Net.Bindings.ZKWeb.System.Drawing

控制器代码:

using Microsoft.AspNetCore.Mvc;
using System.DrawingCore;
using System.DrawingCore.Imaging;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.ZKWeb; 

namespace JCT.Web.Controllers
{
    public class TestController : Controller
    { 
        private Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnv;

        public TestController( Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnv)
        { 
            this.hostingEnv = hostingEnv;
        }

        public ActionResult Index()
        {
            string text = "http://www.niunan.net";
     
            int width = 300;
            int height = 300;

            int heig = width;
            if (width > height)
            {
                heig = height;
                width = height;
            }
            if (string.IsNullOrWhiteSpace(text))
            {
                return null;
            }
            var w = new QRCodeWriter();
            BitMatrix b = w.encode(text, BarcodeFormat.QR_CODE, width, heig);
            var zzb = new BarcodeWriter();
            zzb.Options = new EncodingOptions()
            {
                Margin = 0,
            };
            Bitmap b2 = zzb.Write(b);

            // 将Bitmap转换为字节数组  

            using (MemoryStream memoryStream = new MemoryStream())
            {

                b2.Save(memoryStream, ImageFormat.Jpeg);

                byte[] imageBytes = memoryStream.ToArray();



                // 设置HTTP响应的Content-Type  

                Response.ContentType = "image/jpeg";



                // 将字节数组写入输出流  

                return File(imageBytes, "image/jpeg");

            }
        }


 
    }
}