From 41fe20a6b2658311018865e7393069b97bcbe837 Mon Sep 17 00:00:00 2001 From: Apflkuacha Date: Thu, 20 Jun 2024 00:00:10 +0200 Subject: [PATCH 1/2] Updated Bitmap header Updated the Bitmap header to BITMAPINFOHEADER according to https://en.wikipedia.org/wiki/BMP_file_format#DIB_header_(bitmap_information_header) Slightly improved, now with bitmap size in header. Updating the header was necessary as inserting the previous version inside a PDF document wasn't possible. --- QRCoder/BitmapByteQRCode.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/QRCoder/BitmapByteQRCode.cs b/QRCoder/BitmapByteQRCode.cs index feecdd9a..e3eae657 100644 --- a/QRCoder/BitmapByteQRCode.cs +++ b/QRCoder/BitmapByteQRCode.cs @@ -63,7 +63,7 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightC List bmp = new List(); //header - bmp.AddRange(new byte[] { 0x42, 0x4D, 0x4C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00 }); + bmp.AddRange(new byte[] { 0x42, 0x4D, 0x4C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00 }); //width bmp.AddRange(IntTo4Byte(sideLength)); @@ -72,9 +72,10 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightC //header end bmp.AddRange(new byte[] { 0x01, 0x00, 0x18, 0x00 }); + bmp.AddRange(new byte[24]); //draw qr code - for (var x = sideLength-1; x >= 0; x = x - pixelsPerModule) + for (var x = sideLength - 1; x >= 0; x = x - pixelsPerModule) { for (int pm = 0; pm < pixelsPerModule; pm++) { @@ -97,9 +98,12 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightC } } - //finalize with terminator - bmp.AddRange(new byte[] { 0x00, 0x00 }); - + // write filesize in header + var bmpFileSize = IntTo4Byte(bmp.Count); + for (int i = 0; i < bmpFileSize.Length; i++) + { + bmp[2 + i] = bmpFileSize[i]; + } return bmp.ToArray(); } @@ -125,9 +129,11 @@ private byte[] HexColorToByteArray(string colorString) /// Returns the integer as a 4-byte array. private byte[] IntTo4Byte(int inp) { - byte[] bytes = new byte[2]; + byte[] bytes = new byte[4]; unchecked { + bytes[3] = (byte)(inp >> 24); + bytes[2] = (byte)(inp >> 16); bytes[1] = (byte)(inp >> 8); bytes[0] = (byte)(inp); } From 5c5b5a3957c9121b4aab7538ad36d3cd76585455 Mon Sep 17 00:00:00 2001 From: Raffael Herrmann Date: Sat, 22 Jun 2024 12:28:01 +0200 Subject: [PATCH 2/2] Fixed formatting (dotnet format checks) --- QRCoder/BitmapByteQRCode.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/QRCoder/BitmapByteQRCode.cs b/QRCoder/BitmapByteQRCode.cs index 3aa333f6..529cc5cc 100644 --- a/QRCoder/BitmapByteQRCode.cs +++ b/QRCoder/BitmapByteQRCode.cs @@ -93,7 +93,7 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightC } } } - + // write filesize in header var bmpFileSize = IntTo4Byte(bmp.Count); for (int i = 0; i < bmpFileSize.Length; i++)