Avalonia中使用Zxing.Net生成二维码,一维码

Nuget引两个包
csharp 复制代码
  <Image Name="image"/>
csharp 复制代码
Avalonia.Media.Imaging.Bitmap bitmap = null;
if (item.Barcode == BarcodeType.QR_Code)
{
    var stream = CreateQRCode(item.Text, (int)width, (int)height);
    bitmap = new Avalonia.Media.Imaging.Bitmap(stream);
    image.Source = bitmap;
}
csharp 复制代码
    public static Stream CreateQRCode(string sContent, int width, int height,string encoding = "UTF-8")
    {
        var renderer = new SKBitmapRenderer();
        renderer.Background = SKColors.Transparent;
        renderer.Foreground = SKColors.Black;
        
        BarcodeWriter<SKBitmap> writer = new BarcodeWriter<SKBitmap>
        { 
            Renderer = renderer,
            Format = BarcodeFormat.QR_CODE,
            Options = new QrCodeEncodingOptions
            {
                CharacterSet = encoding,
                Margin = 1,
                DisableECI = true,
                Height = height,
                Width = width,
                ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.L,
            }
        };
        var skBitmap = writer.Write(sContent);
        Stream stream = SKImage.FromBitmap(skBitmap).Encode(SKEncodedImageFormat.Png, 100).AsStream();
        return stream;
    }
相关推荐
上位机付工2 小时前
C#与倍福TwinCAT3进行ADS通信
开发语言·c#
土了个豆子的3 小时前
02.继承MonoBehaviour的单例模式基类
开发语言·visualstudio·单例模式·c#·里氏替换原则
疯狂的维修3 小时前
c#中public类比博图
c#·自动化
土了个豆子的6 小时前
03.缓存池
开发语言·前端·缓存·visualstudio·c#
somethingGoWay12 小时前
wpf .netcore 导出docx文件
wpf·.netcore
somethingGoWay14 小时前
wpf .netcore 导出pdf文件
pdf·wpf·.netcore
xiaowu0801 天前
策略模式-不同的鸭子的案例
开发语言·c#·策略模式
VisionPowerful1 天前
九.弗洛伊德(Floyd)算法
算法·c#
ArabySide1 天前
【C#】 资源共享和实例管理:静态类,Lazy<T>单例模式,IOC容器Singleton我们该如何选
单例模式·c#·.net core
gc_22991 天前
C#测试调用OpenXml操作word文档的基本用法
c#·word·openxml