快速截屏 window 的替代方法

Alternative to taking rapid screenshots of a window

我有一个正在与其他几个人一起测试的实用程序,它会截取另一个 window 的屏幕截图,然后使用 OpenCV 在该屏幕截图中查找较小的图像。

这没有问题,但是,我正在努力提高它的效率,并且想知道,而不是每隔 X 毫秒截取 window 的屏幕截图,如果有办法的话"stream" 屏幕到我的应用程序,然后 运行 针对每个新帧的函数。

这是我当前的代码:

    public static bool ContainsImage(Detection p_Detection, out long elapsed)
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();

        Image<Gray, byte> imgHaystack = new Image<Gray, byte>(CaptureApplication(p_Detection.WindowTitle));
        Image<Gray, byte> imgNeedle = new Image<Gray, byte>(p_Detection.Needle);

        if (imgHaystack.Width >= p_Detection.Settings.Resolution || imgHaystack.Height >= p_Detection.Settings.Resolution)
        {
            imgHaystack = imgHaystack.Resize(imgHaystack.Width / p_Detection.Settings.Scale, imgHaystack.Height / p_Detection.Settings.Scale, Emgu.CV.CvEnum.Inter.Area);
            imgNeedle = imgNeedle.Resize(imgNeedle.Width / p_Detection.Settings.Scale, imgNeedle.Height / p_Detection.Settings.Scale, Emgu.CV.CvEnum.Inter.Area);
        }

        if (imgNeedle.Height < imgHaystack.Height && imgNeedle.Width < imgHaystack.Width)
        {
            using (Image<Gray, float> result = imgHaystack.MatchTemplate(imgNeedle, Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed))
            {
                result.MinMax(out double[] minValues, out double[] maxValues, out Point[] minLocations, out Point[] maxLocations);

                if (maxValues[0] > p_Detection.Settings.MatchThreshold)
                {
                    stopWatch.Stop();
                    elapsed = stopWatch.ElapsedMilliseconds;

                    imgHaystack.Dispose();
                    imgNeedle.Dispose();
                    return true;
                }
            }
        }

        stopWatch.Stop();
        elapsed = stopWatch.ElapsedMilliseconds;

        imgHaystack.Dispose();
        imgNeedle.Dispose();
        return false;
    }

我不完全确定这是完成我正在尝试的最有效的方法,任何帮助都会很棒。

谢谢。

也许你可以使用 Desktop Window ManagerDwmRegisterThumbnail

看看下面的example,看看它是否适合你。

这是一种流式传输另一个 window 图像的非常快速的方法,就像任务栏中的预览图像一样。虽然不确定它是否适用于图像分析。