随笔 - 21  文章 - 0  评论 - 0  阅读 - 3341

Liunx下通过netcore接口生成前端图片的问题。

用netcore来生成前端微信Native支付的二维码。

1、首先CentOS7.0要安装 libgdiplus ,命令如下:yum install libgdiplus-devel ,然后重启netcore服务。//这个地方要注意,网上有不少例子的下载命令是错的,有的时候安装不上。

2、Vs代码使用QRCoder库,代码如下

 public static MemoryStream GerQrCodeStream(string paystr,int pixels = 0)
 {
   QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
   QRCodeData qrCodeData = qrGenerator.CreateQrCode(paystr, QRCodeGenerator.ECCLevel.Q);
   QRCoder.QRCode qrcode = new QRCoder.QRCode(qrCodeData);

   // qrcode.GetGraphic 方法可参考最下发“补充说明”
   var qrCodeImage = qrcode.GetGraphic(pixels);    
   MemoryStream ms = new(); 
   qrCodeImage.Save(ms, format: ImageFormat.Png);//这个地方会提示 Imager.Save仅在windows上受支持。可以不用管。不是错误。  
   return new MemoryStream(ms.GetBuffer());

 }

3、核心问题:System.Drawing.Common.dll必须要降到6.0。之前用System.Drawing.Common.dll 8.0版本一直不成功,报 The type initializer for "Gdip" threw an exception.System.PlatformNotSupportedException: System.Drawing.Common is not supported on non-Windows platforms.

在生成的项目中,会有一个x1yun.runtimeconfig.json文件。x1yun是我的项目名称,文件中的位置要加入

{
  "runtimeOptions": {
    "tfm": "net6.0",
    "frameworks": [
      {
        "name": "Microsoft.NETCore.App",
        "version": "6.0.0"
      },
      {
        "name": "Microsoft.AspNetCore.App",
        "version": "6.0.0"
      }
    ],
    "configProperties": {
      "System.GC.Server": true,
      "System.Reflection.Metadata.MetadataUpdater.IsSupported": false,
      "System.Reflection.NullabilityInfoContext.IsSupported": true,
      "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false,
      "System.Drawing.EnableUnixSupport": true
    }
  }
}

QRCoder库是1.4.1

项目的目标框架是net6.0 。

估计是需要与目标框架一致才可以用System.Drawing.Common

posted on   侗家小蚁哥  阅读(37)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示