Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing PNG data directly to SvgLogo #491

Merged
merged 4 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion QRCoder/SvgQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetBuffer() returns the current working memory buffer allocated by MemoryStream, including extra padding that is reserved but not yet used by the MemoryStream. This is unlike ToArray which copies the working data to a new array of the correct length. To properly use GetBuffer, the length must be passed to Convert.ToBase64String so that only the working data is encoded.

}
}
_mediaType = MediaType.PNG;
Expand All @@ -312,6 +312,22 @@ public SvgLogo(string iconVectorized, int iconSizePercent = 15, bool fillLogoBac
_isEmbedded = iconEmbedded;
}

/// <summary>
/// Create a logo object to be used in SvgQRCode renderer
/// </summary>
/// <param name="iconRasterized">Logo to be rendered as PNG</param>
/// <param name="iconSizePercent">Degree of percentage coverage of the QR code by the logo</param>
/// <param name="fillLogoBackground">If true, the background behind the logo will be cleaned</param>
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;
}

/// <summary>
/// Returns the raw logo's data
/// </summary>
Expand Down
23 changes: 21 additions & 2 deletions QRCoderTests/SvgQRCodeRendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#if NET5_0
AppDomain.CurrentDomain.BaseDirectory;
#else
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\", "");

Check warning on line 24 in QRCoderTests/SvgQRCodeRendererTests.cs

View workflow job for this annotation

GitHub Actions / build

'Assembly.CodeBase' is obsolete: 'Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility. Use Assembly.Location.'

Check warning on line 24 in QRCoderTests/SvgQRCodeRendererTests.cs

View workflow job for this annotation

GitHub Actions / build

'Assembly.CodeBase' is obsolete: 'Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility. Use Assembly.Location.'
#endif
}

Expand Down Expand Up @@ -106,7 +106,7 @@
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
[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();
Expand All @@ -120,10 +120,29 @@
var svg = new SvgQRCode(data).GetGraphic(10, Color.DarkGray, Color.White, logo: logoObj);

var result = HelperFunctions.StringToHash(svg);
result.ShouldBe("78e02e8ba415f15817d5ed88c4afca31");
result.ShouldBe("78e02e8ba415f15817d5ed88c4afca31");
}
#endif

[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>(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");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can see here that the new constructor results in the same hash as using the System.Drawing.Bitmap constructor.

Copy link
Contributor Author

@Shane32 Shane32 Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this is false. Well, I was sorta surprised; .NET should re-encode the PNG with the Bitmap constructor. Not sure why I thought otherwise.

}

[Fact]
[Category("QRRenderer/SvgQRCode")]
public void can_render_svg_qrcode_with_svg_logo_embedded()
Expand Down
Loading