From 204b7e7b3569f8e712421817a5d6d658b4c65c64 Mon Sep 17 00:00:00 2001 From: Manuel Bl Date: Sun, 10 Dec 2023 18:23:35 +0100 Subject: [PATCH 1/4] Support for transparent background with SkiaSharp --- Demo-SkiaSharp/Program.cs | 7 ++++++- Demo-SkiaSharp/QrCodeBitmapExtensions.cs | 24 +++++++++++------------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Demo-SkiaSharp/Program.cs b/Demo-SkiaSharp/Program.cs index 487d19e..ce6e04d 100644 --- a/Demo-SkiaSharp/Program.cs +++ b/Demo-SkiaSharp/Program.cs @@ -9,6 +9,7 @@ using System; using System.IO; +using SkiaSharp; namespace Net.Codecrete.QrCodeGenerator.Demo { @@ -21,9 +22,13 @@ internal static void Main() var filename = "hello-world-QR.png"; var qr = QrCode.EncodeText(text, QrCode.Ecc.Medium); // Create the QR code symbol + qr.SaveAsPng(filename, scale: 10, border: 4); + Console.WriteLine($"QR code has been saved as {Path.GetFullPath(filename)}"); - Console.WriteLine($"The QR code has been saved as {Path.GetFullPath(filename)}"); + filename = "transparent-QR.png"; + qr.SaveAsPng(filename, 10, 4, SKColors.DarkBlue, SKColors.Transparent); + Console.WriteLine($"QR code has been saved as {Path.GetFullPath(filename)}"); } } } diff --git a/Demo-SkiaSharp/QrCodeBitmapExtensions.cs b/Demo-SkiaSharp/QrCodeBitmapExtensions.cs index dad2bea..6e1a651 100644 --- a/Demo-SkiaSharp/QrCodeBitmapExtensions.cs +++ b/Demo-SkiaSharp/QrCodeBitmapExtensions.cs @@ -39,27 +39,25 @@ public static SKBitmap ToBitmap(this QrCode qrCode, int scale, int border, SKCol } // create bitmap - SKBitmap bitmap = new SKBitmap(dim, dim, SKColorType.Rgb888x, SKAlphaType.Opaque); + bool usesTransparency = background.Alpha != 255; + var bitmap = new SKBitmap(dim, dim, + usesTransparency ? SKColorType.Rgba8888 : SKColorType.Rgb888x, + usesTransparency ? SKAlphaType.Premul : SKAlphaType.Opaque); - using (SKCanvas canvas = new SKCanvas(bitmap)) + using (var canvas = new SKCanvas(bitmap)) { // draw background - using (SKPaint paint = new SKPaint { Color = background }) - { - canvas.DrawRect(0, 0, dim, dim, paint); - } + canvas.Clear(background); // draw modules - using (SKPaint paint = new SKPaint { Color = foreground }) + using var paint = new SKPaint { Color = foreground }; + for (int y = 0; y < size; y++) { - for (int y = 0; y < size; y++) + for (int x = 0; x < size; x++) { - for (int x = 0; x < size; x++) + if (qrCode.GetModule(x, y)) { - if (qrCode.GetModule(x, y)) - { - canvas.DrawRect((x + border) * scale, (y + border) * scale, scale, scale, paint); - } + canvas.DrawRect((x + border) * scale, (y + border) * scale, scale, scale, paint); } } } From 11101a700960be031fd587a3fe1ad40df8167415 Mon Sep 17 00:00:00 2001 From: Manuel Bl Date: Sun, 10 Dec 2023 20:12:29 +0100 Subject: [PATCH 2/4] Upgrade Image Magick --- Demo-ImageMagick/Demo-ImageMagick.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Demo-ImageMagick/Demo-ImageMagick.csproj b/Demo-ImageMagick/Demo-ImageMagick.csproj index e33f228..dd29573 100644 --- a/Demo-ImageMagick/Demo-ImageMagick.csproj +++ b/Demo-ImageMagick/Demo-ImageMagick.csproj @@ -9,7 +9,7 @@ - + From 90d8173c26e2feee29590e364ba993f976e7e1e3 Mon Sep 17 00:00:00 2001 From: Jon Galloway Date: Fri, 9 Feb 2024 12:30:15 -0800 Subject: [PATCH 3/4] Enable trimming Enable trimming. Multitargeting since [.NET Standard doesn't support trimming](https://learn.microsoft.com/dotnet/core/compatibility/sdk/8.0/trimming-unsupported-targetframework) --- QrCodeGenerator/QrCodeGenerator.csproj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/QrCodeGenerator/QrCodeGenerator.csproj b/QrCodeGenerator/QrCodeGenerator.csproj index bf44320..98b307e 100644 --- a/QrCodeGenerator/QrCodeGenerator.csproj +++ b/QrCodeGenerator/QrCodeGenerator.csproj @@ -1,7 +1,8 @@ - netstandard2.0 + netstandard2.0;net6.0 + true Net.Codecrete.QrCodeGenerator Net.Codecrete.QrCodeGenerator 2.0.4 From b0a714011a11a2c50eddea0728d90510f1053a52 Mon Sep 17 00:00:00 2001 From: "Manuel Bl." Date: Sat, 23 Mar 2024 15:50:44 +0100 Subject: [PATCH 4/4] Packages.lock for multi-targeting --- QrCodeGenerator/packages.lock.json | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/QrCodeGenerator/packages.lock.json b/QrCodeGenerator/packages.lock.json index 98ad6e6..e6312c7 100644 --- a/QrCodeGenerator/packages.lock.json +++ b/QrCodeGenerator/packages.lock.json @@ -36,6 +36,40 @@ "resolved": "1.1.1", "contentHash": "WMcGpWKrmJmzrNeuaEb23bEMnbtR/vLmvZtkAP5qWu7vQsY59GqfRJd65sFpBszbd2k/bQ8cs8eWawQKAabkVg==" } + }, + "net6.0": { + "Microsoft.NET.ILLink.Analyzers": { + "type": "Direct", + "requested": "[7.0.100-1.23211.1, )", + "resolved": "7.0.100-1.23211.1", + "contentHash": "0GvbEgDGcUQA9KuWcQU1WwYHXt1tBzNr1Nls/M57rM7NA/AndFwCaCEoJpJkmxRY7xLlPDBnmGp8h5+FNqUngg==" + }, + "Microsoft.NET.ILLink.Tasks": { + "type": "Direct", + "requested": "[7.0.100-1.23211.1, )", + "resolved": "7.0.100-1.23211.1", + "contentHash": "tvG8XZYLjT0o3WicCyKBZysVWo1jC9HdCFmNRmddx3WbAz0UCsd0qKZqpiEo99VLA8Re+FzWK51OcRldQPbt2Q==" + }, + "Microsoft.SourceLink.GitHub": { + "type": "Direct", + "requested": "[1.*, )", + "resolved": "1.1.1", + "contentHash": "IaJGnOv/M7UQjRJks7B6p7pbPnOwisYGOIzqCz5ilGFTApZ3ktOR+6zJ12ZRPInulBmdAf1SrGdDG2MU8g6XTw==", + "dependencies": { + "Microsoft.Build.Tasks.Git": "1.1.1", + "Microsoft.SourceLink.Common": "1.1.1" + } + }, + "Microsoft.Build.Tasks.Git": { + "type": "Transitive", + "resolved": "1.1.1", + "contentHash": "AT3HlgTjsqHnWpBHSNeR0KxbLZD7bztlZVj7I8vgeYG9SYqbeFGh0TM/KVtC6fg53nrWHl3VfZFvb5BiQFcY6Q==" + }, + "Microsoft.SourceLink.Common": { + "type": "Transitive", + "resolved": "1.1.1", + "contentHash": "WMcGpWKrmJmzrNeuaEb23bEMnbtR/vLmvZtkAP5qWu7vQsY59GqfRJd65sFpBszbd2k/bQ8cs8eWawQKAabkVg==" + } } } } \ No newline at end of file