0

Using C# and Visual Studios

I am trying to find any documentation on how 8-bit greyscale images are encoded but no amount of research has resulted in any information. When we convert a bmp file (created with a forced depth of 8-bit), of a single black pixel, into a byte array and display it. It prints 1KB worth of data. It makes no sense and there is no documentation online for 8bpp formatting with headers and padding etc.

Why is it 1KB for a single pixel? How is it encoded? Are we making some fundamental error?

// Function to convert image types to byte arrays
        public static byte[] ImageToByte(System.Drawing.Image img)
        {
            System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
            return (byte[])converter.ConvertTo(img, typeof(byte[]));
        }

 // Print bytes to console
            int iterationCount = 1;
            foreach (byte element in convertedBitmap)
            {
                Console.WriteLine(element + ": Byte " + iterationCount);
                iterationCount++;
            }

// Import image from file
            System.Drawing.Image squarePic = System.Drawing.Image.FromFile("C:/.../8bitBlack.bmp");

// Convert image to bitmap
            Bitmap myBitmap = new Bitmap(squarePic);
            Bitmap clone = myBitmap.Clone(new System.Drawing.Rectangle(0, 0, myBitmap.Width, myBitmap.Height), System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

// Set color pallette of bitmap
            ColorPalette palette = clone.Palette;
            for (int i = 0; i < 256; i++)
                palette.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);
            clone.Palette = palette;

// Convert image to byte array
            byte[] convertedBitmap = ImageToByte(clone);
  • 1
    Did you try to decode the fileformat? reading the headers? – Jeroen van Langen Feb 16 '20 at 21:42
  • It's possible your data includes the palette, which would come in right at 1kB. – 3Dave Feb 16 '20 at 21:47
  • See https://stackoverflow.com/questions/18959585/imageconverter-byte-a – 3Dave Feb 16 '20 at 21:50
  • Does this answer your question? [ImageConverter byte a](https://stackoverflow.com/questions/18959585/imageconverter-byte-a) – 3Dave Feb 16 '20 at 21:50
  • You're not getting the bytes that define the color information. You're retrieving the bytes that define an Image Format (PNG, in this case), including all the overhead that comes with it. If you just want the Color information (the bytes that compose the Image Pixels), use [`Bitmap.LockBits()`](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap.lockbits) (see the simple example there). – Jimi Feb 16 '20 at 22:03
  • An indexed format will just return the index to the Palette table, though, so you have to work with both the pixels' bytes and the Palette, side-by-side. Maybe explain what is the that you're actually trying to accomplish here. – Jimi Feb 16 '20 at 22:09
  • Maybe this is useful: [How to determine if an Image is Grayscale](https://stackoverflow.com/a/49481035/7444103) (the class there doesn't just determine whether an image is GrayScale, give it a look) – Jimi Feb 16 '20 at 22:14

0 Answers0