Skip to content

Commit fc48785

Browse files
Apflkuachacodebude
andauthored
Updated Bitmap header (#565)
* 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. * Fixed formatting (dotnet format checks) --------- Co-authored-by: Raffael Herrmann <[email protected]>
1 parent 81fad40 commit fc48785

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

QRCoder/BitmapByteQRCode.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightC
5959
var bmp = new List<byte>();
6060

6161
//header
62-
bmp.AddRange(new byte[] { 0x42, 0x4D, 0x4C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00 });
62+
bmp.AddRange(new byte[] { 0x42, 0x4D, 0x4C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00 });
6363

6464
//width
6565
bmp.AddRange(IntTo4Byte(sideLength));
@@ -68,6 +68,7 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightC
6868

6969
//header end
7070
bmp.AddRange(new byte[] { 0x01, 0x00, 0x18, 0x00 });
71+
bmp.AddRange(new byte[24]);
7172

7273
//draw qr code
7374
for (var x = sideLength - 1; x >= 0; x -= pixelsPerModule)
@@ -93,12 +94,16 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightC
9394
}
9495
}
9596

96-
//finalize with terminator
97-
bmp.AddRange(new byte[] { 0x00, 0x00 });
98-
97+
// write filesize in header
98+
var bmpFileSize = IntTo4Byte(bmp.Count);
99+
for (int i = 0; i < bmpFileSize.Length; i++)
100+
{
101+
bmp[2 + i] = bmpFileSize[i];
102+
}
99103
return bmp.ToArray();
100104
}
101105

106+
102107
/// <summary>
103108
/// Converts a hex color string to a byte array.
104109
/// </summary>
@@ -121,9 +126,11 @@ private byte[] HexColorToByteArray(string colorString)
121126
/// <returns>Returns the integer as a 4-byte array.</returns>
122127
private byte[] IntTo4Byte(int inp)
123128
{
124-
byte[] bytes = new byte[2];
129+
byte[] bytes = new byte[4];
125130
unchecked
126131
{
132+
bytes[3] = (byte)(inp >> 24);
133+
bytes[2] = (byte)(inp >> 16);
127134
bytes[1] = (byte)(inp >> 8);
128135
bytes[0] = (byte)(inp);
129136
}

0 commit comments

Comments
 (0)