Convierta el mapa de bits en blanco y negro en una matriz booleana [duplicado]

Posible duplicado:
¿Una forma rápida de convertir un mapa de bits en una matriz booleana en 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.

Aquí está mi solución actual:

    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.

preguntado el 12 de junio de 12 a las 16:06

Revisa la respuesta aquí stackoverflow.com/a/4235768/529282 -

You may get a c-style pointer to the data. Take a look at Bitmap.LockBits msdn.microsoft.com/en-us/library/5ey6h79d.aspx -

thanks, this is just what I was looking for. -

1 Respuestas

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

Respondido el 12 de junio de 12 a las 16:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.