public static Point PixelSearch(Utils.RECT rect, Color cl, double diff = 0) { if (System.Environment.OSVersion.Version.Major == 6) { return(PixelSearchWin7(rect, cl, diff)); } IntPtr hdc = GetDC(IntPtr.Zero); for (int y = rect.top; y < rect.bottom; y++) { for (int x = rect.left; x < rect.right; x++) { uint pixel = GetPixel(hdc, x, y); Color c = intToColor(pixel); double d = diffColor(c, cl); //Debug.WriteLine(c + " " + cl + " " + d + "[" + x + "," + y + "]"); if (d <= diff) { ReleaseDC(IntPtr.Zero, hdc); return(new Point(x, y)); } } } ReleaseDC(IntPtr.Zero, hdc); return(new Point(0, 0)); }
public static void drawRect(Utils.RECT rect) { IntPtr hdc = GetDC(IntPtr.Zero); Rectangle(hdc, rect.left - 10, rect.top - 10, rect.right + 10, rect.bottom + 10); ReleaseDC(IntPtr.Zero, hdc); }
public CellType getCellTYpe(Utils.MyRawBitmapData src, Utils.RECT rect) { BitmapSearch.BitmapSearchResult ret = cellTypeSearch.Search(src, rect, 2, 100); if (ret != null) { return(htJelly[ret.bitmap]); } return(CellType.Normal); }
public static Point SmartFindBitmap(MyRawBitmapData src, Utils.RECT rect, MyRawBitmapData pattern, int step = 5, double error1 = 10000, double error2 = 100000) { List <Point> lst = GetSubPositions(src, pattern, rect, step, error1); if (lst.Count > 0) { return(lst[0]); } return(new Point(0, 0)); }
public BitmapSearchResult Search(Utils.MyRawBitmapData data, Utils.RECT rect, int step = 1, double colorError = 1, bool flgSingle = true) { foreach (KeyColorMyRawBitmapData b in lstBitmap.Keys) { List <Point> listP = GetSubPositions(data, b, rect, step, colorError, flgSingle); if (listP.Count > 0) { return(new BitmapSearchResult(lstBitmap[b], listP[0])); } } return(null); }
public CellType getCellTYpe(Bitmap src, Utils.RECT rect) { foreach (Bitmap b in htJelly.Keys) { Point pRet = Utils.findBitmap(src, rect, b, 2, 100, 100); if (pRet.X != 0) { return(htJelly[b]); } } return(CellType.Normal); }
private void button4_Click(object sender, EventArgs e) { screen = new Bitmap("candycrush.png"); Bitmap pngImage = new Bitmap("candycrush/ref.png"); Utils.RECT rect = Utils.RECT.fromInt(0, 0, screen.Width, screen.Height); long start = System.Environment.TickCount; Utils.RECT scanRect = new Utils.RECT(); scanRect.left = rect.left; scanRect.top = rect.top; scanRect.right = (rect.left + rect.right) / 4; scanRect.bottom = (rect.top + rect.bottom) / 3; refPoint = Utils.SmartFindBitmap(screen, scanRect, pngImage); if (refPoint.X == 0) { txtDebug.AppendText("Not found"); return; } refPoint.X += 106; refPoint.Y += 69; Bitmap crop = Utils.cropImage(screen, new Rectangle(refPoint.X, refPoint.Y, cellW * NUMCELL, cellH * NUMCELL)); Utils.RECT CELLRECT = Utils.RECT.fromInt(0, 0, cellW, cellH); for (int i = 0; i < NUMCELL; i++) { for (int j = 0; j < NUMCELL; j++) { board.table[i, j] = null; Rectangle cellRect = new Rectangle((i * cellW), (j * cellH), cellW, cellH); Bitmap cropCell = Utils.cropImage(crop, cellRect); Utils.MyRawBitmapData cropCellBitmapData = new Utils.MyRawBitmapData(cropCell); BitmapSearch.BitmapSearchResult searchRet = candySearch.Search(cropCellBitmapData, CELLRECT, 2, 1000); if (searchRet != null) { Point pRet = searchRet.point; board.table[i, j] = htBmp[searchRet.bitmap].clone(); CellType ct = getCellTYpe(cropCellBitmapData, CELLRECT); board.cell[i, j] = ct; } } } long end = System.Environment.TickCount; dbgLine(refPoint + " " + (end - start)); mainRect = new Rectangle(refPoint.X, refPoint.Y, NUMCELL * cellW, NUMCELL * cellH); currentBestMove = board.getBestMove(); lstResult = board.getPossibleMove(); imgMain.Refresh(); }
public static Point findBitmap(Bitmap src, Utils.RECT rect, Bitmap pattern, int step = 5, double error1 = 10000, double error2 = 100000) { Point p = new Point(0, 0); double minD = 100000000; for (int y = rect.top; y < rect.bottom - pattern.Height; y++) { for (int x = rect.left; x < rect.right - pattern.Width; x++) { double count = 0; for (int i = 0; i < pattern.Width; i += step) { for (int j = 0; j < pattern.Height; j += step) { Color cPattern = pattern.GetPixel(i, j); Color cScr = src.GetPixel(x + i, y + j); double d = diffColor(cPattern, cScr); if (d > error1) { i = pattern.Width; count = minD; break; } count += d; if (count > error2) { i = pattern.Width; count = minD; break; } } } if (count < minD) { minD = count; p.X = x; p.Y = y; //Console.WriteLine("x=" + x + " y=" + y + " min=" + minD + " count=" + count); if (minD == 0) { return(p); } } } //Console.WriteLine(" y=" + y + " min=" + minD); } //Console.WriteLine("p=" + p + " min=" + minD); return(p); }
public static Point PixelSearchWin7(Utils.RECT rect, Color cl, double diff = 0) { Bitmap bmp = GetScreenShot(); double minD = 100000; for (int y = rect.top; y < rect.bottom; y++) { for (int x = rect.left; x < rect.right; x++) { Color c = bmp.GetPixel(x, y); double d = diffColor(c, cl); if (d < minD) { minD = d; if (d <= diff) { return(new Point(x, y)); } } } } Debug.WriteLine(" " + minD + "\n"); return(new Point(0, 0)); }
public static List <Point> GetSubPositions(Utils.MyRawBitmapData main, KeyColorMyRawBitmapData sub, Utils.RECT rect, int step = 1, double colorError = 1, bool flgSingle = true) { List <Point> possiblepos = new List <Point>(); int subwidth = sub.width; int subheight = sub.height; int movewidth = rect.right - subwidth; int moveheight = rect.bottom - subheight; int maxX = rect.left + movewidth - subwidth; int maxY = rect.top + moveheight - subheight; int cX = subwidth / 2; int cY = subheight / 2; if (sub.keyColor != null) { cX = sub.keyColor.pos.X; cY = sub.keyColor.pos.Y; } Utils.MyColor firstColor = sub.GetColor(0, 0); Utils.MyColor CenterColor = sub.GetColor(cX, cY); for (int y = rect.top; y < moveheight; ++y) { for (int x = rect.left; x < movewidth; ++x) { Utils.MyColor curcolor = main.GetColor(x, y); if (possiblepos.Count > 0) { foreach (var item in possiblepos.ToArray()) { int xsub = x - item.X; int ysub = y - item.Y; if (xsub >= subwidth || ysub >= subheight || xsub < 0) { continue; } Utils.MyColor subcolor = sub.GetColor(xsub, ysub); if (!curcolor.nearColor(subcolor, colorError)) { possiblepos.Remove(item); } } } // add part // we should not add pixel that will out of bound if (x > maxX) { continue; } if (y > maxY) { continue; } if (curcolor.nearColor(firstColor, colorError)) { if (CenterColor.nearColor(main.GetColor(x + cX, y + cY), colorError)) { possiblepos.Add(new Point(x, y)); } } } if (flgSingle && (possiblepos.Count > 0)) { if (y - subheight > possiblepos[0].Y) { //Console.WriteLine("found break"); break; } } //Console.WriteLine("Y=" + y + " Count=" + possiblepos.Count); } return(possiblepos); }
private void button1_Click(object sender, EventArgs e) { String title = "Diamond Dash"; Color[] refColors = new Color[] { Color.FromArgb(0xEBDEE4), // Color.FromArgb(235,223,228), }; if (Utils.activateWindow(title)) { Utils.RECT rect = new Utils.RECT(); IntPtr hwnd = Utils.GetForegroundWindow(); Utils.GetWindowRect(hwnd, out rect); //int x = rect.left; //int y = rect.top; //int w = rect.right; //int h = rect.bottom; txtDebug.AppendText("Active " + rect + " " + hwnd + "\n"); Utils.RECT scanRect = new Utils.RECT(); scanRect.left = rect.left; scanRect.top = rect.top; scanRect.right = (rect.left + rect.right) / 3; scanRect.bottom = (rect.top + rect.bottom) / 2; //Int32[] pt = new Int32[2]; for (int i = 0; i < refColors.Length; i++) { long start = System.Environment.TickCount; Point p = Utils.PixelSearch(scanRect, refColors[i], 1); if ((p.X + p.Y) != 0) { Utils.MouseMove(p.X, p.Y); txtDebug.AppendText("Pos " + p + " in " + (System.Environment.TickCount - start) + "\n"); refPoint = p; adjustPoint(); chkStart.Enabled = true; break; } /* try * { * * Array.Copy((System.Object[])(autoit.PixelSearch(x, y, w, h, refColors[i], 0)), pt, 2); * txtDebug.AppendText("Pos " + pt[0] + " " + pt[1] + "\n"); * refPoint.X = pt[0]; * refPoint.Y = pt[1]; * adjustPoint(); * chkStart.Enabled = true; * break; * } * catch (Exception ex) * { * //return; * } */ } } }
public static Point findBitmap(Utils.RECT rect, Bitmap pattern) { return(findBitmap(GetScreenShot(), rect, pattern)); }
private void button1_Click(object sender, EventArgs e) { String title = "Candy Crush"; Candy[,] table = board.table; Color[] refColors = new Color[] { Color.FromArgb(0xEBDEE4), // Color.FromArgb(235,223,228), }; if (Utils.activateWindow(title)) { screen = Utils.GetScreenShot(); Bitmap pngImage = new Bitmap("candycrush/ref.png"); Utils.RECT rect = new Utils.RECT(); IntPtr hwnd = Utils.GetForegroundWindow(); Utils.GetWindowRect(hwnd, out rect); long start = System.Environment.TickCount; txtDebug.AppendText("Active " + rect + " " + hwnd + "\n"); Utils.RECT scanRect = new Utils.RECT(); scanRect.left = rect.left; scanRect.top = rect.top; scanRect.right = (rect.left + rect.right) / 4; scanRect.bottom = (rect.top + rect.bottom) / 3; refPoint = Utils.SmartFindBitmap(screen, scanRect, pngImage); if (refPoint.X <= 0) { txtDebug.AppendText("Not found"); return; } refPoint.X += 106; refPoint.Y += 69; mainRect = new Rectangle(refPoint.X, refPoint.Y, NUMCELL * cellW, NUMCELL * cellH); screen = saveGetScreenShot(mainRect); screen.Save("candycrush.png"); //Utils.MouseMove(refPoint.X, refPoint.Y); Bitmap crop = Utils.cropImage(screen, new Rectangle(refPoint.X, refPoint.Y, cellW * NUMCELL, cellH * NUMCELL)); Utils.RECT CELLRECT = Utils.RECT.fromInt(0, 0, cellW, cellH); for (int i = 0; i < NUMCELL; i++) { for (int j = 0; j < NUMCELL; j++) { board.table[i, j] = null; Rectangle cellRect = new Rectangle((i * cellW), (j * cellH), cellW, cellH); Bitmap cropCell = Utils.cropImage(crop, cellRect); Utils.MyRawBitmapData cropCellBitmapData = new Utils.MyRawBitmapData(cropCell); BitmapSearch.BitmapSearchResult searchRet = candySearch.Search(cropCellBitmapData, CELLRECT, 2, 1000); if (searchRet != null) { Point pRet = searchRet.point; board.table[i, j] = htBmp[searchRet.bitmap].clone(); CellType ct = getCellTYpe(cropCellBitmapData, CELLRECT); board.cell[i, j] = ct; } } } } board.save("table.txt"); board.load("table.txt"); currentBestMove = board.getBestMove(); lstResult = board.getPossibleMove(); imgMain.Refresh(); //txtDebug.AppendText(table.ToString()); }