我正在制作一个 XNA 应用程序,我每秒从网络摄像头捕获屏幕截图 4 次,然后当像素颜色红色低于某个阈值时,我尝试将其转换为布尔数组。当我将其转换为 Texture2D 时,它不会滞后,但是当我尝试获取单个像素时它确实滞后,即使网络摄像头分辨率为 176x144。
这是获取位图的代码:
public Bitmap getBitmap()
{
if (!panelVideoPreview.IsDisposed)
{
Bitmap b = new Bitmap(panelVideoPreview.Width, panelVideoPreview.Height, PixelFormat.Format32bppRgb);
using (Graphics g = Graphics.FromImage(b))
{
Rectangle rectanglePanelVideoPreview = panelVideoPreview.Bounds;
Point sourcePoints = panelVideoPreview.PointToScreen(new Point(panelVideoPreview.ClientRectangle.X, panelVideoPreview.ClientRectangle.Y));
g.CopyFromScreen(sourcePoints, Point.Empty, rectanglePanelVideoPreview.Size);
}
return b;
}
else
{
Bitmap b = new Bitmap(panelVideoPreview.Width, panelVideoPreview.Height);
return b;
}
}
这是将位图转换为布尔数组的代码:
public bool[,] getBoolBitmap(uint treshold)
{
Bitmap b = getBitmap();
bool[,] ar = new bool[b.Width, b.Height];
for (int y = 0; y < b.Height; y++)
{
for (int x = 0; x < b.Width; x++)
{
if (b.GetPixel(x, y).R < treshold)
{
ar[x, y] = false;
}
else
{
ar[x, y] = true;
}
}
}
return ar;
}