From 348e0a93f949a368e30069f15466bcef44f63f61 Mon Sep 17 00:00:00 2001 From: Kris Vandermotten Date: Sun, 12 Jul 2026 16:56:08 +0200 Subject: [PATCH 1/2] Fix bug and a minor performance improvement in BitmapByteQRCode --- QRCoder/BitmapByteQRCode.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/QRCoder/BitmapByteQRCode.cs b/QRCoder/BitmapByteQRCode.cs index 0ca6c5ed..9c3063bd 100644 --- a/QRCoder/BitmapByteQRCode.cs +++ b/QRCoder/BitmapByteQRCode.cs @@ -1,3 +1,8 @@ +#if NETCOREAPP2_1_OR_GREATER +using System.Buffers.Binary; +#endif +using System.Diagnostics; + using static QRCoder.QRCodeGenerator; namespace QRCoder; @@ -67,8 +72,10 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightC moduleLight[i + 2] = lightColorRgb[0]; } + // The size of each row must be rounded up to a multiple of 4 bytes by padding. // Pre-calculate padding bytes - var paddingLen = sideLength % 4; + var paddingLen = -sideLength & 3; + Debug.Assert(paddingLen >= 0 && paddingLen < 4 && (sideLength + paddingLen) % 4 == 0); // Calculate filesize (header + pixel data + padding) var fileSize = 54 + (3 * (sideLength * sideLength)) + (sideLength * paddingLen); @@ -142,6 +149,9 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightC /// Destination byte array that receives the bytes private static void CopyIntAs4ByteToArray(int inp, int destinationIndex, byte[] destinationArray) { +#if NETCOREAPP2_1_OR_GREATER + BinaryPrimitives.WriteInt32LittleEndian(destinationArray.AsSpan(destinationIndex), inp); +#else unchecked { destinationArray[destinationIndex + 3] = (byte)(inp >> 24); @@ -149,6 +159,7 @@ private static void CopyIntAs4ByteToArray(int inp, int destinationIndex, byte[] destinationArray[destinationIndex + 1] = (byte)(inp >> 8); destinationArray[destinationIndex + 0] = (byte)(inp); } +#endif } } From 405403a77b029cfa9af3fec4d266641869e042ab Mon Sep 17 00:00:00 2001 From: Kris Vandermotten Date: Sun, 12 Jul 2026 17:16:14 +0200 Subject: [PATCH 2/2] Fix bug --- QRCoder/BitmapByteQRCode.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/QRCoder/BitmapByteQRCode.cs b/QRCoder/BitmapByteQRCode.cs index 9c3063bd..a46e1949 100644 --- a/QRCoder/BitmapByteQRCode.cs +++ b/QRCoder/BitmapByteQRCode.cs @@ -74,11 +74,12 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightC // The size of each row must be rounded up to a multiple of 4 bytes by padding. // Pre-calculate padding bytes - var paddingLen = -sideLength & 3; - Debug.Assert(paddingLen >= 0 && paddingLen < 4 && (sideLength + paddingLen) % 4 == 0); + var rowByteLength = sideLength * 3; + var paddingLen = -rowByteLength & 3; + Debug.Assert(paddingLen >= 0 && paddingLen < 4 && (rowByteLength + paddingLen) % 4 == 0); // Calculate filesize (header + pixel data + padding) - var fileSize = 54 + (3 * (sideLength * sideLength)) + (sideLength * paddingLen); + var fileSize = 54 + (rowByteLength + paddingLen) * sideLength; // Bitmap container byte[] bmp = new byte[fileSize];