Convierta el mapa de bits en blanco y negro en una matriz booleana [duplicado]
Frecuentes
Visto 1,974 veces
2
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.
1 Respuestas
2
Use Bitmap.LockBits. You'll find tutorials on the web.
Respondido el 12 de junio de 12 a las 16:06
Por ejemplo aquí: stackoverflow.com/a/1563170/284240 - Tim Schmelter
Revisa la respuesta aquí stackoverflow.com/a/4235768/529282 - Martheen
You may get a c-style pointer to the data. Take a look at Bitmap.LockBits msdn.microsoft.com/en-us/library/5ey6h79d.aspx - mortb
thanks, this is just what I was looking for. - Ryan