public static Rectangle[] GetTileRects(Bitmap screenShot) { var screen = new FastBitmapWrapper(screenShot); var rects = new List <Rectangle>(); InRectFunction inRect = delegate(int x, int y) { foreach (var rect in rects) { if ((x >= rect.Left) && (x <= rect.Right) && (y >= rect.Top) && (y <= rect.Bottom)) { return(true); } } return(false); }; var searchOffsets = new List <Point> { new Point(-1, -1), new Point(0, -1), new Point(1, -1), new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(-1, 1), new Point(-1, 0) }; FindNextPixelFunction findNextPixel = delegate( int prevX, int prevY, int currX, int currY, out int nextX, out int nextY) { nextX = -1; nextY = -1; Point prevOffset = new Point(prevX - currX, prevY - currY); int prevOffsetIndex = searchOffsets.IndexOf(prevOffset); List <Point> offsets = new List <Point>(searchOffsets); while (prevOffsetIndex >= 0) { var p = offsets[0]; offsets.RemoveAt(0); offsets.Add(p); prevOffsetIndex--; } for (var index = 0; index < offsets.Count; index++) { var offset = offsets[index]; nextX = currX + offset.X; nextY = currY + offset.Y; if ((nextX < 0) || (nextX >= screen.Width) || (nextY < 0) || (nextY >= screen.Height)) { continue; } if (screen.GetPixel(nextX, nextY) == Settings.NormalTileColor) { return(true); } } return(false); }; for (var y = 0; y < screen.Height; y++) { for (var x = 0; x < screen.Width; x++) { if (inRect(x, y)) { continue; } if (screen.GetPixel(x, y) == Settings.NormalTileColor) { int tileLeft = x; int tileRight = x; int tileTop = y; int tileBottom = y; int prevX = x - 1; int prevY = y; int currX = x; int currY = y; int nextX; int nextY; while (findNextPixel(prevX, prevY, currX, currY, out nextX, out nextY)) { if ((nextX == x) & (nextY == y)) { // We've looped right back to the start break; } tileLeft = Math.Min(tileLeft, nextX); tileRight = Math.Max(tileRight, nextX); tileTop = Math.Min(tileTop, nextY); tileBottom = Math.Max(tileBottom, nextY); prevX = currX; prevY = currY; currX = nextX; currY = nextY; } Rectangle tileRect = new Rectangle(tileLeft, tileTop, tileRight - tileLeft, tileBottom - tileTop); if ((tileRect.Width > 0) && (tileRect.Height > 0)) { rects.Add(tileRect); } } } } return(rects.ToArray()); }
public static Rectangle[] GetTileRects(Bitmap screenShot) { var screen = new FastBitmapWrapper(screenShot); var rects = new List<Rectangle>(); InRectFunction inRect = delegate(int x, int y) { foreach (var rect in rects) { if ((x >= rect.Left) && (x <= rect.Right) && (y >= rect.Top) && (y <= rect.Bottom)) return true; } return false; }; var searchOffsets = new List<Point> { new Point(-1, -1), new Point(0, -1), new Point(1, -1), new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(-1, 1), new Point(-1, 0) }; FindNextPixelFunction findNextPixel = delegate( int prevX, int prevY, int currX, int currY, out int nextX, out int nextY) { nextX = -1; nextY = -1; Point prevOffset = new Point(prevX - currX, prevY - currY); int prevOffsetIndex = searchOffsets.IndexOf(prevOffset); List<Point> offsets = new List<Point>(searchOffsets); while (prevOffsetIndex >= 0) { var p = offsets[0]; offsets.RemoveAt(0); offsets.Add(p); prevOffsetIndex--; } for (var index = 0; index < offsets.Count; index++) { var offset = offsets[index]; nextX = currX + offset.X; nextY = currY + offset.Y; if ((nextX < 0) || (nextX >= screen.Width) || (nextY < 0) || (nextY >= screen.Height)) continue; if (screen.GetPixel(nextX, nextY) == Settings.NormalTileColor) return true; } return false; }; for (var y = 0; y < screen.Height; y++) { for (var x = 0; x < screen.Width; x++) { if (inRect(x, y)) continue; if (screen.GetPixel(x, y) == Settings.NormalTileColor) { int tileLeft = x; int tileRight = x; int tileTop = y; int tileBottom = y; int prevX = x - 1; int prevY = y; int currX = x; int currY = y; int nextX; int nextY; while (findNextPixel(prevX, prevY, currX, currY, out nextX, out nextY)) { if ((nextX == x) & (nextY == y)) { // We've looped right back to the start break; } tileLeft = Math.Min(tileLeft, nextX); tileRight = Math.Max(tileRight, nextX); tileTop = Math.Min(tileTop, nextY); tileBottom = Math.Max(tileBottom, nextY); prevX = currX; prevY = currY; currX = nextX; currY = nextY; } Rectangle tileRect = new Rectangle(tileLeft, tileTop, tileRight - tileLeft, tileBottom - tileTop); if ((tileRect.Width > 0) && (tileRect.Height > 0)) rects.Add(tileRect); } } } return rects.ToArray(); }