【问题标题】:Check if exact image in exact location is on screen检查确切位置的确切图像是否在屏幕上
【发布时间】:2016-07-14 20:41:58
【问题描述】:

我希望在 Visual Studio (C#) 中创建一个程序,该程序会在屏幕的确切位置扫描屏幕以获取准确的图像。我已经看到许多讨论涉及找到“接近”图像的算法,但我的将是 100% 准确的;位置、大小等等。

我使用以下代码从屏幕的一部分 [图 1] 获得了一个 png:

private void button1_Click(object sender, EventArgs e)
    {
        //Create a new bitmap.
        var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                       Screen.PrimaryScreen.Bounds.Height);

        // Create a graphics object from the bitmap.
        var gfxScreenshot = Graphics.FromImage(bmpScreenshot);

        // Take the screenshot from the upper left corner to the right bottom corner.
        gfxScreenshot.CopyFromScreen(1555, 950, 
                                    1700, 1010,
                                    Screen.PrimaryScreen.Bounds.Size,
                                    CopyPixelOperation.SourceCopy);

        // Save the screenshot to the specified path that the user has chosen.
        bmpScreenshot.Save("Screenshot.png");
    }

所以,基本上这是我的程序流程图,说明我想如何前进: 1)使用上面的代码创建主png 2)运行循环: 使用与主 png 相同的过程创建相同的屏幕截图 比较主 png 和新的截图 png 和 if:match 然后继续,否则重复循环。

我对编程很陌生,但我不相信这超出了我的能力范围,只要有一点指导。我编写了相当复杂的(在我看来)VBA 和 Matlab 程序。任何帮助是极大的赞赏。

谢谢你, 斯隆

【问题讨论】:

    标签: image bitmap screen screenshot


    【解决方案1】:

    通过微软的文档挖掘了一下,我想出了一个粗略的函数,它可以做一些类似于你想要的事情。 https://msdn.microsoft.com/en-us/library/hh191601.aspx

    这个函数提供了陷入无限循环的机会,所以你可以考虑在你的主函数中调用它。有关具有超时的同步方法的信息,请参见此处: Monitoring a synchronous method for timeout

    从你的 main 中,你所要做的就是看看它是否返回 true。

    static int Main(string[] args)
    {
       if (ImageInLocation(left, right, top, bottom)) {
          // do other things
       }
    
       return 0;
    }
    

    我唯一不能完全确定的是,您对 ColorDifference 的要求有多严格。即使图像是相同的,任何具有完全不容忍 ColorDifference 的像素差异都会出现错误。如果您知道它应该起作用而它不是,也许考虑增加公差。以下是有关此的更多信息: https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.uitesting.colordifference.aspx

    public bool ImageInLocation(int left, int right, int top, int bottom) {
       bool image_found = false;
       var masterImage = Image.FromFile("path_to_master");
    
       while (!image_found) {
          // screenshot code above, output to "path_to/Screenshot.jpg"
          var compImage = Image.FromFile("path_to/Screenshot.jpg");
    
          // note, all zeroes may not be tolerant enough
          var color_diff = new ColorDifference(0, 0, 0, 0); 
          Image diffImage;
    
          image_found = ImageComparer.Compare(masterImage, compImage, color_diff, out diffImage);
       }
    
       return true;
    }
    

    祝你好运!欢迎来到编程社区。​​p>

    另外,如果有人有任何建议/更改,请随时编辑。快乐的成像,朋友们!

    【讨论】:

      猜你喜欢
      • 2020-04-08
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 2013-03-30
      • 2017-09-30
      • 1970-01-01
      • 2014-12-19
      • 2021-11-01
      相关资源
      最近更新 更多