Possible Duplicate:
Fast way to convert a Bitmap into a Boolean array in C#?
在我的项目中,我有一个黑白位图资源,我用它来保存一些 4x4 黑白 Sprite 。在我可以有效地使用这些数据之前,我需要将其转换为二维多维(或锯齿状,无关紧要) boolean 数组,其中 false 代表白色,黑色代表 true。
这是我当前的解决方案:
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;
}
}
}
由于我会多次调用此函数(使用不同的位图),是否有更有效的方法来执行此操作? .GetPixel 有点慢,感觉我在这里错过了一些技巧。感谢您的任何建议。
最佳答案
使用 Bitmap.LockBits。您可以在网上找到教程。
关于c# - 将黑白位图转换为 boolean 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10999652/