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 小时前
unity实现梦日记式传送组件
后端·c#·unity3d
阿蒙Amon2 小时前
C#扩展方法全解析:给现有类型插上翅膀的魔法
开发语言·c#
qq_3923971213 小时前
Redis常用操作
数据库·redis·wpf
三千道应用题16 小时前
WPF学习笔记(25)MVVM框架与项目实例
wpf
江山如画,佳人北望16 小时前
C#程序入门
开发语言·windows·c#
与火星的孩子对话17 小时前
Unity进阶课程【六】Android、ios、Pad 终端设备打包局域网IP调试、USB调试、性能检测、控制台打印日志等、C#
android·unity·ios·c#·ip
future141217 小时前
C#每日学习日记
java·学习·c#
军训猫猫头21 小时前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net
厦门德仔1 天前
【WPF】WPF(样式)
android·java·wpf
葬歌倾城2 天前
JSON的缩进格式方式和紧凑格式方式
c#·json