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,852
  • 4
  • 18
  • 18
  • Check the answer here http://stackoverflow.com/a/4235768/529282 – Martheen Jun 12 '12 at 15:20
  • You may get a c-style pointer to the data. Take a look at Bitmap.LockBits http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx – mortb Jun 12 '12 at 15:23

1 Answers1

2

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

usr
  • 168,620
  • 35
  • 240
  • 369