【发布时间】:2012-06-15 12:17:13
【问题描述】:
可能重复:
Fast way to convert a Bitmap into a Boolean array in C#?
在我的项目中,我有一个资源是一个黑白位图,我用它来保存一些 4x4 黑白精灵。在我可以有效地使用这些数据之前,虽然我需要将其转换为 2D 多维(或锯齿状,没关系)布尔数组,其中 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 有点慢,感觉就像我在这里错过了一些技巧。感谢您的任何建议。
【问题讨论】:
-
您可能会得到一个指向数据的 c 样式指针。看看 Bitmap.LockBits msdn.microsoft.com/en-us/library/5ey6h79d.aspx
-
谢谢,这正是我想要的。
标签: c# arrays bitmap boolean imaging