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);