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;
    }
相关推荐
张人玉1 天前
c#串口读写威盟士五插针
开发语言·c#·通讯
睡前要喝豆奶粉1 天前
在.NET Core Web Api中使用redis
redis·c#·.netcore
偶尔的鼠标人1 天前
SqlSugar查询字符串转成Int的问题
c#·sqlsugar
我不是程序猿儿1 天前
【C#】WinForms 控件句柄与 UI 刷新时机
开发语言·ui·c#
聪明努力的积极向上1 天前
【C#】HTTP中URL编码方式解析
开发语言·http·c#
关关长语1 天前
(四) Dotnet中MCP客户端与服务端交互通知日志信息
ai·c#·mcp
小码编匠1 天前
WPF 动态模拟CPU 使用率曲线图
后端·c#·.net
聪明努力的积极向上1 天前
【.NET】依赖注入浅显解释
c#·.net
hixiong1231 天前
C# OpencvSharp使用lpd_yunet进行车牌检测
开发语言·opencv·计算机视觉·c#
beyond谚语1 天前
第一章 WPF概述
wpf