2

Possible Duplicate:
Fast way to convert a Bitmap into a Boolean array in C#?

in my project I have a resource that is a black and white bitmap which I'm using to hold some 4x4 black and white sprites. Before I can use this data effectively though I need to convert it to a 2D multidimensional (or jagged, doesn't matter) boolean array, with false representing white and black representing true.

Here is my current solution:

    public Bitmap PiecesBitmap = Project.Properties.Resources.pieces;
    bool[,] PiecesBoolArray = new bool[4, 16]; // 4 wide, 16 high (4 4x4 images)

    for (int x = 0; x < 4; x++)
            {
                for (int y = 0; y < 16; y++)
                {
                    if (PiecesBitmap.GetPixel(x, y) == Color.Black)
                    {
                        PiecesBoolArray[x, y] = true;
                    }
                    else
                    {
                        PiecesBoolArray[x, y] = false;
                    }

                }
            }

Since I will be calling this function a lot (with different bitmaps), is there a more efficient way of doing this? .GetPixel is kind of slow, and it just feels like I'm missing out on some trick here. Thank you for any suggestions.

Community
  • 1
  • 1
Ryan
  • 3,642
  • 4
  • 15
  • 18

1 Answers1

2

Use Bitmap.LockBits. You'll find tutorials on the web.

usr
  • 165,323
  • 34
  • 234
  • 359