public static List <Point> locateTaskBarPrograms(Image screen, int xOff, int yOff) { // pattern Image originalScreen = screen; Image pattern = iconPattern; // reduce if real pattern not set if (pattern == Image.Empty) { pattern = AssemblyTools.getAssemblyImage("taskbar_icon.png"); ColorReducer reducer = new ColorReducers.TaskBar(); pattern = reducer.reduceColors(pattern); screen = reducer.reduceColors(screen); } // locate pattern Dictionary <int, List <Point> > icons = new Dictionary <int, List <Point> >(); for (int y = 0; y < screen.height - pattern.height; y++) { for (int x = 0; x < screen.width - pattern.width; x++) { Image sub = screen.crop(x, x + pattern.width, y, y + pattern.height); if (ImageTools.match(sub, pattern)) { // remember pattern if (iconPattern == Image.Empty) { iconPattern = originalScreen.crop(x, x + pattern.width, y, y + pattern.height); } // store Log.Debug("found taskbar_icon.png pattern x=" + x + ", y=" + y); Point point = new Point(xOff + x, yOff + y); bool found = false; // check for near y foreach (int knownY in icons.Keys) { if (Math.Abs(y - knownY) < 5) { icons[knownY].Add(point); found = true; break; } } // add new y if (!found) { icons.Add(y, new List <Point>() { new Point(xOff + x, yOff + y) }); } // skip x x += pattern.width; } } } // sort Dictionary <int, List <Point> > .ValueCollection sorted = icons.Values; List <Point> result = new List <Point>(); foreach (List <Point> points in sorted) { var sortedX = from p in points orderby p.X ascending select p; result.AddRange(sortedX); } return(result); }
public static Point locateUnknownTable(Image screen, List <Point> offsets, TableLayout layout) { // vars Image originalScreen = screen; Image pattern = tablePattern; DateTime start = DateTime.Now; // reduce or use exact image if (pattern == Image.Empty) { // read from assembly Log.Debug("reading table pattern"); pattern = AssemblyTools.getAssemblyImage("table_top_left_corner.png"); // reduce colors Log.Debug("reducing pattern colors"); ColorReducer reducer = new ColorReducers.Table(); screen = reducer.reduceColors(screen); pattern = reducer.reduceColors(pattern); } // first line int[] patternFirstLine = pattern.lines[0]; // locate pattern for (int y = 0; y < screen.height - pattern.height; y++) { int[] screenLine = screen.lines[y]; for (int x = 0; x < screen.width - pattern.width; x++) { // check first line if (match(patternFirstLine, screenLine, x)) { // first corner Image cornerTL = screen.crop(x, x + pattern.width, y, y + pattern.height); if (ImageTools.match(cornerTL, pattern)) { // second corner Image cornerBR = originalScreen.crop(x + layout.CornerBottom.X, x + layout.CornerBottom.X + layout.CornerBottom.Width, y + layout.CornerBottom.Y, y + layout.CornerBottom.Y + layout.CornerBottom.Height); if (isTableBottomRightCornerVisible(cornerBR)) { // remember exact pattern if (tablePattern == Image.Empty) { tablePattern = originalScreen.crop(x, x + pattern.width, y, y + pattern.height); } // return location Point point = new Point(x, y); if (!offsets.Contains(point)) { Log.Info("found new table pattern x=" + x + ", y=" + y + " took " + DateTime.Now.Subtract(start).TotalMilliseconds + "ms"); return(point); } } } } } } return(Point.Empty); }