public Point PixelSearchPoint(Bitmap b, Color PixelColor, int Shade_Variation)
{
Color Pixel_Color = PixelColor;
Point Pixel_Coords = new Point(-1, -1);
using (Bitmap RegionIn_Bitmap = (Bitmap)b.Clone())
{
BitmapData RegionIn_BitmapData = RegionIn_Bitmap.LockBits(new Rectangle(0, 0, RegionIn_Bitmap.Width, RegionIn_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int[] Formatted_Color = new int[3] { Pixel_Color.B, Pixel_Color.G, Pixel_Color.R }; //bgr
unsafe
{
for (int y = 0; y < RegionIn_BitmapData.Height; y++)
{
byte* row = (byte*)RegionIn_BitmapData.Scan0 + (y * RegionIn_BitmapData.Stride);
for (int x = 0; x < RegionIn_BitmapData.Width; x++)
{
if (row[x * 3] >= (Formatted_Color[0] - Shade_Variation) & row[x * 3] <= (Formatted_Color[0] + Shade_Variation)) //blue
{
if (row[(x * 3) + 1] >= (Formatted_Color[1] - Shade_Variation) & row[(x * 3) + 1] <= (Formatted_Color[1] + Shade_Variation)) //green
{
if (row[(x * 3) + 2] >= (Formatted_Color[2] - Shade_Variation) & row[(x * 3) + 2] <= (Formatted_Color[2] + Shade_Variation)) //red
{
Pixel_Coords = new Point(x, y);
RegionIn_Bitmap.Dispose();
goto End;
}
}
}
}
}
}
}
End:
b.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
return Pixel_Coords;
}
这个代码从位图搜索找到颜色代码
using (Bitmap b = new Bitmap(NativeHelper.GetOriginalWinScreen(handle)))
{
Point p = PixelSearchPoint(b, c, 0);
if (p.X != -1 && p.Y != -1)
{
Logwrite(p.X + "/" + p.Y + " Click With" + c.ToString());
chk = true;
ClickPos(p.X, p.Y);
notecomplete = true;
counttest++;
}
b.Dispose();
}
这个代码内存在访问不安全方法时如此快速泄漏 GC.Collect和GC.WaitForPendingFinalizers();不使用UnSafe方法??
答案 0 :(得分:0)
如果调用RegionIn_Bitmap.LockBits,则需要确保在处置之前调用UnlockBits。