0

I have a simple black & white .bmp file and i would like to convert it through C# into a series of 0s and 1s, excluding the file headers and padding. The image is 32x32 pixels and this is what I have tried so far, but i couldn't remove the padding and header informations.

public BitArray imageToBinaryArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    bool[] arr = new bool[50000000];
    int i = 0;
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
    BitArray bitarray = new BitArray(ms.ToArray());
    return bitarray;
}

private void readImage()
{
    BitArray data;
    string path = @"C:\Users\me\dummy.bmp";
    Image Dummy = Image.FromFile(path);
    data = imageToBinaryArray(Dummy);

    var sb = new StringBuilder();

    for (int i = 0; i < data.Count; i++)
    {
        char c = data[i] ? '1' : '0';
        sb.Append(c);
    }
    string s = sb.ToString();
    Console.WriteLine(s);
}

You can see below the figure i want to translate.

dummy bmp

This is what i want to print:

00000000000000000000000000000000
11111111111111111111111111111111
01010101010101010101010101010101
10101010101010101010101010101010
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
00000000000000000000000000000000
00000000000000000000000000000001
00000000000000000000000000000011
00000000000000000000000000000111
00000000000000000000000000001111
11111111111111111111111111111111
11110000000000000000000000000000
11100000000000000000000000000000
11000000000000000000000000000000
10000000000000000000000000000000
00000000000000000000000000000000
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111

Thanks in advance to everyone who will try to help me!

lizzi
  • 13
  • 2
  • A binary bmp image is not simply a series of 0 and 1s that each represent one pixel. A pixel is still (probably) 3 values: RGB. You are just using only 2 of them: 0x000000 and 0xFFFFFF. So you'd need to binarize each pixel, to get 0s and 1s. – Fildor Mar 17 '22 at 10:58
  • [Bitmap.LockBits()](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap.lockbits) is commonly used to parse the BitmapData of an Image. A simple example here: [How to make colors in a Palette transparent when drawing 8bpp Bitmaps](https://stackoverflow.com/a/65498663/7444103) – Jimi Mar 17 '22 at 11:08

2 Answers2

0

As explained here, you can read the image into a Bitmap. Then iterate through the pixels and write them to your textfile.

using System.Drawing;

Bitmap img = new Bitmap("*imagePath*");
for (int i = 0; i < img.Width; i++)
{
    for (int j = 0; j < img.Height; j++)
    {
        Color pixel = img.GetPixel(i,j);

        if (pixel == *somecondition*)
        {
            **Store pixel here in a array or list or whatever** 
        }
    }
} 
Axel Kemper
  • 10,544
  • 2
  • 31
  • 54
0

Try following :

            byte[] input = {0x00, 0x00, 0x00, 0x00,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0x55, 0x55, 0x55, 0x55,
                            0xAA, 0xAA, 0xAA, 0xAA,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0x00, 0x00, 0x00, 0x00,
                            0x00, 0x00, 0x00, 0x01,
                            0x00, 0x00, 0x00, 0x03,
                            0x00, 0x00, 0x00, 0x0F,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xF0, 0x00, 0x00, 0x00,
                            0xE0, 0x00, 0x00, 0x00,
                            0xC0, 0x00, 0x00, 0x00,
                            0x80, 0x00, 0x00, 0x00,
                            0x00, 0x00, 0x00, 0x00,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF
                           };
            string[] doubleWords = input.Select((x,i) => new {b = x, index = i})
                .GroupBy(x => x.index/4)
                .Select(x => string.Join("",x.Select(y => Convert.ToString(y.b, 2).PadLeft(8,'0'))))
                .ToArray();
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • thank you, this is very useful, but i was looking for something more general, so that it could work with any image of similar type (still 1-bit and monochrome) – lizzi Mar 17 '22 at 11:37
  • The code works with any byte array. You have to get the bytes from the image. The divide by 4 is grouping the array into 32 bits. If you need a different size than you have to change the divide value. If the data is 16 bits instead of 8 bits than you need to change the padding from 8 to 16 and change the divide from 4 to 2. – jdweng Mar 17 '22 at 12:21
  • Look at the bimap format in Wiki : https://en.wikipedia.org/wiki/BMP_file_format?force_isolation=true – jdweng Mar 17 '22 at 12:26