From ed0c16f6c90d8e77af9f6f6ce38cffda2b4c8ec4 Mon Sep 17 00:00:00 2001 From: Shane32 Date: Tue, 11 Jun 2024 18:27:25 -0400 Subject: [PATCH 1/2] Add overload for color to PngByteQRCode --- QRCoder/PngByteQRCode.cs | 13 ++++++++++ .../QRCoder.approved.txt | 1 + .../net60-windows/QRCoder.approved.txt | 1 + QRCoderApiTests/net60/QRCoder.approved.txt | 1 + QRCoderTests/PngByteQRCodeRendererTests.cs | 24 +++++++++++++++++++ 5 files changed, 40 insertions(+) diff --git a/QRCoder/PngByteQRCode.cs b/QRCoder/PngByteQRCode.cs index 757b8e05..035e43ae 100644 --- a/QRCoder/PngByteQRCode.cs +++ b/QRCoder/PngByteQRCode.cs @@ -43,6 +43,19 @@ public byte[] GetGraphic(int pixelsPerModule, bool drawQuietZones = true) } } +#if !NETSTANDARD1_3 + /// + /// Creates a 2-color PNG of the QR code, using 1-bit indexed color. Accepts 3-byte RGB colors for normal images and 4-byte RGBA-colors for transparent images. + /// + /// The number of pixels each dark/light module of the QR code will occupy in the final QR code image. + /// The color of the dark modules. + /// The color of the light modules. + /// Indicates if quiet zones around the QR code should be drawn. + /// Returns the QR code graphic as a PNG byte array. + public byte[] GetGraphic(int pixelsPerModule, System.Drawing.Color darkColor, System.Drawing.Color lightColor, bool drawQuietZones = true) + => GetGraphic(pixelsPerModule, new byte[] { darkColor.R, darkColor.G, darkColor.B, darkColor.A }, new byte[] { lightColor.R, lightColor.G, lightColor.B, lightColor.A }, drawQuietZones); +#endif + /// /// Creates a 2-color PNG of the QR code, using 1-bit indexed color. Accepts 3-byte RGB colors for normal images and 4-byte RGBA-colors for transparent images. /// diff --git a/QRCoderApiTests/net35+net40+net50+net50-windows+netstandard20/QRCoder.approved.txt b/QRCoderApiTests/net35+net40+net50+net50-windows+netstandard20/QRCoder.approved.txt index 4bc3a2ff..0a75bc99 100644 --- a/QRCoderApiTests/net35+net40+net50+net50-windows+netstandard20/QRCoder.approved.txt +++ b/QRCoderApiTests/net35+net40+net50+net50-windows+netstandard20/QRCoder.approved.txt @@ -837,6 +837,7 @@ namespace QRCoder public PngByteQRCode(QRCoder.QRCodeData data) { } public byte[] GetGraphic(int pixelsPerModule, bool drawQuietZones = true) { } public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgba, byte[] lightColorRgba, bool drawQuietZones = true) { } + public byte[] GetGraphic(int pixelsPerModule, System.Drawing.Color darkColor, System.Drawing.Color lightColor, bool drawQuietZones = true) { } } public static class PngByteQRCodeHelper { diff --git a/QRCoderApiTests/net60-windows/QRCoder.approved.txt b/QRCoderApiTests/net60-windows/QRCoder.approved.txt index 9f84c603..99b5f961 100644 --- a/QRCoderApiTests/net60-windows/QRCoder.approved.txt +++ b/QRCoderApiTests/net60-windows/QRCoder.approved.txt @@ -844,6 +844,7 @@ namespace QRCoder public PngByteQRCode(QRCoder.QRCodeData data) { } public byte[] GetGraphic(int pixelsPerModule, bool drawQuietZones = true) { } public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgba, byte[] lightColorRgba, bool drawQuietZones = true) { } + public byte[] GetGraphic(int pixelsPerModule, System.Drawing.Color darkColor, System.Drawing.Color lightColor, bool drawQuietZones = true) { } } public static class PngByteQRCodeHelper { diff --git a/QRCoderApiTests/net60/QRCoder.approved.txt b/QRCoderApiTests/net60/QRCoder.approved.txt index a50f60df..6d269d08 100644 --- a/QRCoderApiTests/net60/QRCoder.approved.txt +++ b/QRCoderApiTests/net60/QRCoder.approved.txt @@ -788,6 +788,7 @@ namespace QRCoder public PngByteQRCode(QRCoder.QRCodeData data) { } public byte[] GetGraphic(int pixelsPerModule, bool drawQuietZones = true) { } public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgba, byte[] lightColorRgba, bool drawQuietZones = true) { } + public byte[] GetGraphic(int pixelsPerModule, System.Drawing.Color darkColor, System.Drawing.Color lightColor, bool drawQuietZones = true) { } } public static class PngByteQRCodeHelper { diff --git a/QRCoderTests/PngByteQRCodeRendererTests.cs b/QRCoderTests/PngByteQRCodeRendererTests.cs index 7fc1a7ce..06c3be0d 100644 --- a/QRCoderTests/PngByteQRCodeRendererTests.cs +++ b/QRCoderTests/PngByteQRCodeRendererTests.cs @@ -65,6 +65,30 @@ public void can_render_pngbyte_qrcode_color() #endif } +#if !NETCOREAPP1_1 + [Fact] + [Category("QRRenderer/PngByteQRCode")] + public void can_render_pngbyte_qrcode_drawing_color() + { + //Create QR code + var gen = new QRCodeGenerator(); + var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L); + var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5, Color.Red, Color.Blue); + +#if NETCOREAPP1_1 + var result = HelperFunctions.ByteArrayToHash(pngCodeGfx); + result.ShouldBe("0144b1d40aa6eeb6cb07df42822ea0a7"); +#else + using (var mStream = new MemoryStream(pngCodeGfx)) + { + var bmp = (Bitmap)Image.FromStream(mStream); + var result = HelperFunctions.BitmapToHash(bmp); + result.ShouldBe("88d394b2405499869feb69b81593e703"); + } +#endif + } +#endif + [Fact] [Category("QRRenderer/PngByteQRCode")] From e2659d5726f64db704dbc9e7763c64641e68d9bd Mon Sep 17 00:00:00 2001 From: Shane32 Date: Tue, 11 Jun 2024 18:31:10 -0400 Subject: [PATCH 2/2] Update xml comment --- QRCoder/PngByteQRCode.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/QRCoder/PngByteQRCode.cs b/QRCoder/PngByteQRCode.cs index 035e43ae..25d5ffa9 100644 --- a/QRCoder/PngByteQRCode.cs +++ b/QRCoder/PngByteQRCode.cs @@ -45,7 +45,7 @@ public byte[] GetGraphic(int pixelsPerModule, bool drawQuietZones = true) #if !NETSTANDARD1_3 /// - /// Creates a 2-color PNG of the QR code, using 1-bit indexed color. Accepts 3-byte RGB colors for normal images and 4-byte RGBA-colors for transparent images. + /// Creates a 2-color PNG of the QR code, using 1-bit indexed color. Colors may contain transparency. /// /// The number of pixels each dark/light module of the QR code will occupy in the final QR code image. /// The color of the dark modules.