Ejemplo n.º 1
0
        private Rectangle Check(WindowInfo info, GameIndexTemplate template)
        {
            FileConfig fileConfig = ConfigRepository.fileConfig;
            //获取结束图片模板
            string imagePath     = fileConfig.GetTemplateIndexImagePath(template.TemplateImageName);
            Bitmap templateImage = (Bitmap)Image.FromFile(imagePath);

            //置顶
            Win.SetWindowPos(info.hWnd.hWnd, -1, 0, 0, 0, 0, 1 | 2);
            //var image = Win.GetWindowCapture(windowHanle.hWnd);
            //截图
            var image = Win.CopyScreen(info.Width, info.Height, info.X, info.Y);

            var s_bmp = image;
            var p_bmp = templateImage;

            var bmp_temp_path = fileConfig.WindowImageTempDir;

            string timespan = "";// DateTime.Now.ToString("yyyyMMddHHmmssfff");

            var s_bmp_temp_name = template.TemplateId + "_s.png";
            var p_bmp_temp_name = template.TemplateId + "_p.png";

            //存储截图
            ImageTools.SaveImage(bmp_temp_path + "\\" + timespan, s_bmp_temp_name, s_bmp);
            ImageTools.SaveImage(bmp_temp_path + "\\" + timespan, p_bmp_temp_name, templateImage);
            Stopwatch sp = new Stopwatch();

            sp.Start();
            long      matchTime;
            Rectangle result = ImageTools.Match(
                Path.Combine(bmp_temp_path + "\\" + timespan, s_bmp_temp_name),
                imagePath,
                out matchTime);

            sp.Stop();
            _alert("图片匹配耗时:" + sp.ElapsedMilliseconds + "ms");

            if (!result.IsEmpty) //&& (result.X >= 0 && result.Y >= 0 && result.Width > 0 && result.Height > 0))
            {
                //计算与模板大小差
                if (result.Width < p_bmp.Width || result.Height < p_bmp.Height)
                {
                    return(new Rectangle());
                }

                if (result.Width - p_bmp.Width > 20 || result.Height - p_bmp.Height > 20)
                {
                    return(new Rectangle());
                }
                //找到位置
                _alert(string.Format("找到位置:\r\nx={0}\r\ny={1}\r\nw={2}\r\nh={3}", result.X, result.Y, result.Width, result.Height));
                Graphics g = Graphics.FromImage(s_bmp);
                g.DrawRectangle(new Pen(Color.Red, 2), result);
                ImageTools.SaveImage(bmp_temp_path + "\\" + timespan, template.TemplateId + "_ss.png", s_bmp);
                return(result);
            }
            return(new Rectangle());
        }