我需要用指定的颜色找到它找到的第一个像素的点/坐标(x,y)。我使用了 GetPixel() 方法,但它有点太慢了,正在研究 LockBits。我怎么不知道这是否真的可以解决我的问题。我可以使用 LockBits 返回找到的像素的点吗?
这是我当前的代码:
public Point FindPixel(Image Screen, Color ColorToFind)
{
Bitmap bit = new Bitmap(Screen);
BitmapData bmpData = bit.LockBits(new Rectangle(0, 0, bit.Width, bit.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format32bppPArgb);
unsafe
{
byte* ptrSrc = (byte*)bmpData.Scan0;
for (int y = 0; y < bmpData.Height; y++)
{
for (int x = 0; x < bmpData.Width; x++)
{
Color c = bit.GetPixel(x, y);
if (c == ColorToFind)
return new Point(x, y);
}
}
}
bit.UnlockBits(bmpData);
return new Point(0, 0);
}