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;
    }
相关推荐
唐青枫18 小时前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫2 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m6252 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户91721561902112 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠3 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫5 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech5 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf7 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6257 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech7 天前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf