目前我有这个,我正在搜索 RGB 值(208、94、94),如果匹配,它将告诉我的 DX 设备在屏幕上绘图并将其记录在控制台中。
好吧,我不确定如何获得像素的确切 X、Y 坐标。Y 很好,不是问题,但 X 总是关闭并且值很高。我应该如何获得 X,Y?有公式还是我的代码错了?有没有办法在 LockBits 中使用 GetPixel 并且速度很快?
例如它打印:
x: 3772 y: 187 当实际值为: 944, 187
x: 3888 y: 212 当实际值为: 973, 212
x: 3788 y: 224 当实际值为: 948, 224
unsafe
{
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap("FileName.png"))
{
BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
int bytesPerPixel = System.Drawing.Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;
int heightInPixels = bitmapData.Height;
int widthInBytes = bitmapData.Width * bytesPerPixel;
byte* ptrFirstPixel = (byte*)bitmapData.Scan0;
for (int y = 0; y < heightInPixels; y++)
{
byte* currentLine = ptrFirstPixel + (y * bitmapData.Stride);
for (int x = 0; x < widthInBytes; x = x + bytesPerPixel)
{
int Blue = currentLine[x];
int Green = currentLine[x + 1];
int Red = currentLine[x + 2];
if (Red == 208 & Green == 94 & Blue == 94)
{
// Device.DrawText("Unit", TextFont, new RawRectangleF(x, y, float.MaxValue, float.MaxValue), UnitTextBrush);
Console.WriteLine($"x: {x} y: {y}");
break;
}
}
}
bmp.UnlockBits(bitmapData);
}
}