1

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.

3
  • possible duplicate of Convert a bitmap into a byte array Commented Jul 13, 2015 at 7:06
  • No, it's not. I have also tried to use the converter solution: byte[] imgBytes = (byte[])converter.ConvertTo(inputBmp, typeof(byte[])); but that led to the same result. Commented Jul 13, 2015 at 7:17
  • Why are you taking the image from the picture box? Just use the original file. Oh, and using SoapHexBinary for anything but SOAP hex binaries sounds like a bad idea :) Commented Jul 13, 2015 at 7:35

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks for that advice, I haven't thought about that - it is indeed ovbious now as you explained it, but my experience with bitmap and encoding/decoding-schemes is two days young. Never worked with that before. Can you give me an advice or a code example how to get the raw data of the bitmap? In the end, I want to see and manipulate the hex data (as string).
@EmanuelLeicht At some point, you're reading the data from a file - just read the file directly into a byte array (e.g. use File.ReadAllBytes). You can then use that same byte array to construct the bitmap as well (using MemoryStream on top of the byte array, for example).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.