1

I am trying to write a C# program designed to convert a .bmp file into binary. The file is 16x16 pixels. Each black pixel represents a one in binary, so the number 10 would be █[]█[][][][][]

The problem I am having is that my code is not recognizing the black pixels, so the output is always zero.

    public Bitmap imgToDecode;

    private void button2_Click(object sender, EventArgs e)
    {
        int i = (imgToDecode.Height * imgToDecode.Width);
        bool[] pixData = new bool[i];

        int p = 0;

        for (int k = 1; k < imgToDecode.Height; k++)
        {
            for (int m = 1; m < imgToDecode.Width; m++)
            {
                if (imgToDecode.GetPixel(m, k) == Color.Black)
                {
                    pixData[p] = true;
                }
                else
                {
                    pixData[p] = false;
                }
                p++;
            }
        }

        for (int n = 0; n < pixData.Length; n++)
        {
            textBox2.Text = (textBox2.Text + (Convert.ToInt32(pixData[n])));
        }


    }

If anyone has an idea as to why the output is 0, could they please help me. Also any ways of improving the code would be welcomed.

Krikler7
  • 13
  • 4
  • Have you checked what .GetPixel() is returning? Maybe your 'black' in the source image is actually slightly NON-black, so the pixel might be 255,255,254 and color.black is 255,255,255. – Marc B Dec 01 '11 at 17:59
  • Why on Earth do you want the data in an array of `bool`s? (Just curious) – Cameron Dec 01 '11 at 17:59
  • 3
    At least one mistake is that you start the loops at 1 instead of 0. – Hans Passant Dec 01 '11 at 18:02

3 Answers3

3

The source of the problem is probably that Color.Black is not equal to Color.FromArgb(0, 0, 0).

The solution would probably be to change the line:

if (imgToDecode.GetPixel(m, k) == Color.Black)

to:

if (imgToDecode.GetPixel(m, k) == Color.FromArgb(0, 0, 0))

or even better, declare a variable containing the (0,0,0) color and then use it in this if statement. So do something like:

 Color black = Color.FromArgb(0, 0, 0);

in the beginning of your method and then change if to:

if (imgToDecode.GetPixel(m, k) == black)

UPDATE: There seemed to be some minor issues with loops start values. I've updated your code.

public Bitmap imgToDecode;

private void button2_Click(object sender, EventArgs e)
{
    textBox2.Text = "";

    Color black = Color.FromArgb(0, 0, 0);

    int i = (imgToDecode.Height * imgToDecode.Width);
    bool[] pixData = new bool[i];

    int p = 0;

    for (int k = 0; k < imgToDecode.Height; k++)
    {
        for (int m = 0; m < imgToDecode.Width; m++)
        {
            pixData[p] = (imgToDecode.GetPixel(m, k) == black);

            p++;
        }
    }

    for (int n = 0; n < pixData.Length; n++)
    {
        textBox2.Text = (textBox2.Text + (Convert.ToInt32(pixData[n])));
    }
}

If you don't need pixData array to contain bool values, you can change it to int and asign 1 or 0. This way you don't have to convert it later :).

Lukasz M
  • 5,635
  • 2
  • 22
  • 29
1

You could use this utility to convert your bitmap to binary text. Just check if every pixel channels is equal to 0 and then return 1 (if black) or 0 (if white):

public static IEnumerable<int> ToBinary(this Bitmap imgToDecode)
{
    for (int k = 0; k < imgToDecode.Height; k++)
    {
        for (int m = 0; m < imgToDecode.Width; m++)
        {
            yield return (imgToDecode.GetPixel(m, k).R == 0 && 
                          imgToDecode.GetPixel(m, k).G == 0 && 
                          imgToDecode.GetPixel(m, k).B == 0) ? 1 : 0;
        }
    }
}
as-cii
  • 12,819
  • 4
  • 41
  • 43
0

Make these changes:

for (int m = 1; m < imgToDecode.Width; m++)
{
    Color col = imgToDecode.GetPixel(m, k);
    if (col  == Color.Black)   //<---- Put a breakpoint here and see what col is.
    {
        pixData[p] = true;
    }
    ...

When you break on 'col', see what it is. Is it 255,255,255? Something close?

This at least gets you to see what 'black' pixels vs. the 'white' pixels are, and you can properly switch on that.

EDIT: And Hans points out correctly that you need to start at '0' instead of '1' on those loops.

DanTheMan
  • 3,277
  • 2
  • 21
  • 40