From b8654c64115833eedb49557909dc8f2d1def98a1 Mon Sep 17 00:00:00 2001 From: Shane32 Date: Tue, 2 Apr 2024 02:04:42 -0400 Subject: [PATCH 1/2] Support passing PNG logos directly to SvgLogo --- QRCoder/SvgQRCode.cs | 18 +++++++++++++++++- QRCoderTests/SvgQRCodeRendererTests.cs | 23 +++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/QRCoder/SvgQRCode.cs b/QRCoder/SvgQRCode.cs index e03ce5dc..657b8a9a 100644 --- a/QRCoder/SvgQRCode.cs +++ b/QRCoder/SvgQRCode.cs @@ -285,7 +285,7 @@ public SvgLogo(Bitmap iconRasterized, int iconSizePercent = 15, bool fillLogoBac using (var bitmap = new Bitmap(iconRasterized)) { bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png); - _logoData = Convert.ToBase64String(ms.GetBuffer(), Base64FormattingOptions.None); + _logoData = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length, Base64FormattingOptions.None); } } _mediaType = MediaType.PNG; @@ -311,6 +311,22 @@ public SvgLogo(string iconVectorized, int iconSizePercent = 15, bool fillLogoBac _isEmbedded = iconEmbedded; } + /// + /// Create a logo object to be used in SvgQRCode renderer + /// + /// Logo to be rendered as PNG + /// Degree of percentage coverage of the QR code by the logo + /// If true, the background behind the logo will be cleaned + public SvgLogo(byte[] iconRasterized, int iconSizePercent = 15, bool fillLogoBackground = true) + { + _iconSizePercent = iconSizePercent; + _logoData = Convert.ToBase64String(iconRasterized, Base64FormattingOptions.None); + _mediaType = MediaType.PNG; + _fillLogoBackground = fillLogoBackground; + _logoRaw = iconRasterized; + _isEmbedded = false; + } + /// /// Returns the raw logo's data /// diff --git a/QRCoderTests/SvgQRCodeRendererTests.cs b/QRCoderTests/SvgQRCodeRendererTests.cs index b0620594..50a258bd 100644 --- a/QRCoderTests/SvgQRCodeRendererTests.cs +++ b/QRCoderTests/SvgQRCodeRendererTests.cs @@ -105,7 +105,7 @@ public void can_render_svg_qrcode_without_quietzones_hex() [Fact] [Category("QRRenderer/SvgQRCode")] - public void can_render_svg_qrcode_with_png_logo() + public void can_render_svg_qrcode_with_png_logo_bitmap() { //Create QR code var gen = new QRCodeGenerator(); @@ -119,7 +119,26 @@ public void can_render_svg_qrcode_with_png_logo() var svg = new SvgQRCode(data).GetGraphic(10, Color.DarkGray, Color.White, logo: logoObj); var result = HelperFunctions.StringToHash(svg); - result.ShouldBe("78e02e8ba415f15817d5ed88c4afca31"); + result.ShouldBe("7d53f25af04e52b20550deb2e3589e96"); + } + + [Fact] + [Category("QRRenderer/SvgQRCode")] + public void can_render_svg_qrcode_with_png_logo_bytearray() + { + //Create QR code + var gen = new QRCodeGenerator(); + var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H); + + //Used logo is licensed under public domain. Ref.: https://thenounproject.com/Iconathon1/collection/redefining-women/?i=2909346 + var logoBitmap = System.IO.File.ReadAllBytes(GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"); + var logoObj = new SvgQRCode.SvgLogo(iconRasterized: logoBitmap, 15); + logoObj.GetMediaType().ShouldBe(SvgQRCode.SvgLogo.MediaType.PNG); + + var svg = new SvgQRCode(data).GetGraphic(10, Color.DarkGray, Color.White, logo: logoObj); + + var result = HelperFunctions.StringToHash(svg); + result.ShouldBe("7d53f25af04e52b20550deb2e3589e96"); } [Fact] From 4b8fafd59c208716863aedbb1afb25a588c77680 Mon Sep 17 00:00:00 2001 From: Shane32 Date: Tue, 2 Apr 2024 17:26:16 -0400 Subject: [PATCH 2/2] Update tests --- QRCoderTests/SvgQRCodeRendererTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/QRCoderTests/SvgQRCodeRendererTests.cs b/QRCoderTests/SvgQRCodeRendererTests.cs index b6023c5c..0981e6e2 100644 --- a/QRCoderTests/SvgQRCodeRendererTests.cs +++ b/QRCoderTests/SvgQRCodeRendererTests.cs @@ -120,8 +120,9 @@ public void can_render_svg_qrcode_with_png_logo_bitmap() var svg = new SvgQRCode(data).GetGraphic(10, Color.DarkGray, Color.White, logo: logoObj); var result = HelperFunctions.StringToHash(svg); - result.ShouldBe("7d53f25af04e52b20550deb2e3589e96"); + result.ShouldBe("78e02e8ba415f15817d5ed88c4afca31"); } +#endif [Fact] [Category("QRRenderer/SvgQRCode")] @@ -141,7 +142,6 @@ public void can_render_svg_qrcode_with_png_logo_bytearray() var result = HelperFunctions.StringToHash(svg); result.ShouldBe("7d53f25af04e52b20550deb2e3589e96"); } -#endif [Fact] [Category("QRRenderer/SvgQRCode")]