Reputation: 11
I am opening a .bmp file from disk and then converting it to a byte array. From there, I want to have it as a hex string to be able to show the hex data in a textbox. The problem is, that the output - in the byte array as well as in the string - doesn't compare to the hex data I get when opening the .bmp in notepad++ or a different hex editor.
My code is
// getting image from a Picturebox
Bitmap inputBmp = (Bitmap)pictureBoxInput.Image;
// saving bmp to stream
MemoryStream imgStream = new MemoryStream();
inputBmp.Save(imgStream, inputBmp.RawFormat);
// convert to byte Array and SoapHexBinary
byte[] imgBytes = imgStream.ToArray();
SoapHexBinary imgHexBinary = new SoapHexBinary(imgBytes);
// create String out of HexBinary
string imgHexString = "";
imgHexString = imgHexBinary.ToString();
The resulting string is (formatted):
424D
36190000
0000
0000
36000000
28000000
28000000
28000000
0100
2000
00000000
00000000
10170000
10170000
00000000
00000000
FFE7CB00
FFE7CB00
FFE7CB00
FFE7CB00
FFE7CB00
FFE7CB00
FFE7CB00
FFE7CB00
FFE7CB00
FFE7CB00
FFE7CB00
FFE7CB00
FFE7CB00
FFE7CB00
FFE7CB00
FFE7CB00
FFE7CB00FFE7CB00FFE7CB00FFE7CB00FFE7CB00FFE7CB00FFE7CB00FFE7CB00FFE7CB00...
However the expected output according to notepad++ incl. hex-editor would be:
42 4D
E0 01 00 00
00 00
00 00
7A 00 00 00
28 00 00 00
28 00 00 00
28 00 00 00
01 00
08 00
01 00 00 00
66 01 00 00
10 17 00 00
10 17 00 00
11 00 00 00
11 00 00 00
05 00 02 00
05 09 07 00
18 11 10 00
1F 21 20 00
39 32 31 00
46 3E 3D 00
6E 67 5F 00
7E 77 6F 00
91 8D 83 00
96 95 89 00
BA B2 A7 00
D3 C4 BA 00
F2 DE CE 00
ED E3 C6 00
FF E7 CB 00
FE EC CF 00
FF FF FF 00
28 0E 00 00 28 0E 00 00 28 0E 00 00 28 0E 00 00 28 0E 00 00
28 0E 00 00 28 0E 00 00 28 0E 00 00 28 0E 00 00 17 0E 01 09
01 02 0F 0E 00 00 16 0E 00 03 05 02 02 00 0F 0E 00 00 15 0E
00 04 02 00 00 02 0F 0E 00 00 13 0E 00 06 09 00 00 00 02 02 ...
So can somebody explain why the decoding is different? And it's not only the decoding: In the manually decoded bitmap hex string, there's only one colour that is repeated constantly, the four bytes FFE7CB00 which doesn't make sense at all. What have I missed?
I appreciate your help and advice.
Upvotes: 1
Views: 1101
Reputation: 63722
Your assumption that the bitmap you're saving is the same that you loaded is simply false.
The output data makes this entirely too obvious: for example, your re-encoded bitmap is 32-bit, while the original one was 8-bit with palette. Not to mention that the original file had RLE! As far as I know, GDI+ doesn't support saving RL-encoded bitmaps (though as you've probably noticed, it reads them just fine). In other words, by the time you have a Bitmap
instance, it's already too late - you no longer have the raw file data - you only have the raw bitmap data. There's many ways to encode bitmaps.
When you want to show raw data of a file, just show raw data of a file - don't decode it, re-encode it and display that.
Upvotes: 1