1
我正在製作一個XNA應用程序,我從網絡攝像頭每秒捕獲4次屏幕截圖,然後嘗試在像素顏色紅色低於特定閾值時將其轉換爲布爾數組。當我將它轉換爲Texture2D時,它不會滯後,但是當我嘗試獲取單個像素時,它確實會滯後,即使網絡攝像頭分辨率爲176x144。在C#中將位圖轉換爲布爾型數組的快速方法?
這是搶位圖的代碼:
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;
}
GetPixel()非常慢,它必須鎖定每個像素的位圖數據。改用Bitmap.LockBits()。當你把它放在搜索框中時有很多點擊。 –