/// <summary> /// 是否能在窗口中找到这个图片 /// </summary> public int FindInWindow(bool useDesktopCapture = false) { Bitmap windowScreenshot = getWindowScreenshot(useDesktopCapture); BitmapData parentImageData = windowScreenshot.LockBits(new Rectangle(0, 0, windowScreenshot.Width, windowScreenshot.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); BitmapData subImageData = this.image.LockBits(new Rectangle(0, 0, this.image.Width, this.image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); searchZone = ProcessSearchZone(parentImageData.Width, parentImageData.Height); Stopwatch sw = new Stopwatch(); sw.Start(); //计时开始 Rectangle[] result = new FindImage().Match(parentImageData, subImageData, new Rectangle(searchZone.X, searchZone.Y, searchZone.Width, searchZone.Height)); sw.Stop(); windowScreenshot.UnlockBits(parentImageData); this.image.UnlockBits(subImageData); windowScreenshot.Dispose(); this.parserResult = new Target[result.Length]; for (int i = 0; i < result.Length; i++) { this.parserResult[i] = new Target(result[i], 1); } return(result.Length); }
/// <summary> /// 查找图片过程中,排除一些颜色 /// </summary> /// <param name="similarity">容错值</param> /// <param name="excludeColor">排除的颜色</param> /// <returns></returns> public int FindInWindow(Color excludeColor, int similarity = 0, bool useDesktopCapture = false) { Bitmap windowScreenshot = getWindowScreenshot(useDesktopCapture); BitmapData parentImageData = windowScreenshot.LockBits(new Rectangle(0, 0, windowScreenshot.Width, windowScreenshot.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); BitmapData subImageData = this.image.LockBits(new Rectangle(0, 0, this.image.Width, this.image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); searchZone = ProcessSearchZone(parentImageData.Width, parentImageData.Height); Rectangle[] result; Stopwatch sw = new Stopwatch(); sw.Start(); //计时开始 if (excludeColor.IsEmpty) { result = new FindImage().Match(parentImageData, subImageData, searchZone, similarity); } else { result = new FindImage().Match(parentImageData, subImageData, searchZone, excludeColor, similarity); } sw.Stop(); windowScreenshot.UnlockBits(parentImageData); this.image.UnlockBits(subImageData); //if (useDesktopCapture) //{ // Graphics g = Graphics.FromImage(windowScreenshot); // g.DrawRectangle(new Pen(Color.Red, 1), searchZone.X, searchZone.Y, searchZone.Width, searchZone.Height); // foreach (Rectangle rect in result) // { // g.DrawRectangle(new Pen(Color.Red, 1), rect.X, rect.Y, rect.Width, rect.Height); // } // windowScreenshot.Save(@"C:\Users\Injoy\Desktop\draw.png"); //} windowScreenshot.Dispose(); this.parserResult = new Target[result.Length]; for (int i = 0; i < result.Length; i++) { this.parserResult[i] = new Target(result[i], 1); } return(result.Length); }