Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Created September 25, 2022 07:24
Show Gist options
  • Save guitarrapc/bc11f8d7c2ae3ce4e481aa391b5e0c88 to your computer and use it in GitHub Desktop.
Save guitarrapc/bc11f8d7c2ae3ce4e481aa391b5e0c88 to your computer and use it in GitHub Desktop.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.2" />
<PackageReference Include="SkiaSharp.QrCode" Version="0.6.0" />
</ItemGroup>
<ItemGroup>
<None Update="Untitled.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
using SkiaSharp.QrCode.Models;
using SkiaSharp;
using SkiaSharp.QrCode;
var url = "https://example.com";
var byteIcon = File.ReadAllBytes(@"Untitled.png");
var icon = new IconData
{
Icon = SKBitmap.Decode(byteIcon),
IconSizePercent = 10,
};
using var generator = new QRCodeGenerator();
var qr = generator.CreateQrCode(url, ECCLevel.L);
var info = new SKImageInfo(512, 512);
using var surface = SKSurface.Create(info);
var canvas = surface.Canvas;
canvas.Render(qr, info.Width, info.Height, SKColor.Parse("ffffff"), SKColor.Parse("000000"), icon);
using var image = surface.Snapshot();
using var data = image.Encode(SKEncodedImageFormat.Png, 100);
data.SaveTo(new FileStream($"../Generated_{System.Runtime.InteropServices.RuntimeInformation.OSDescription}.png", FileMode.CreateNew));
@guitarrapc
Copy link
Author

guitarrapc commented Sep 25, 2022

Untitled.png
Untitled

WSL2 Ubuntu 22.04 result
Generated_Linux 4 4 0-22000-Microsoft #653-Microsoft Wed Apr 27 160600 PST 2022

Windows 11 result
Generated_Microsoft Windows 10 0 22000

@guitarrapc
Copy link
Author

Another sample for non-black outline.

Untitled

WSL2 Ubuntu 22.04 result
Generated_Linux 4 4 0-22000-Microsoft #653-Microsoft Wed Apr 27 160600 PST 2022

Windows 11 result
Generated_Microsoft Windows 10 0 22000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment