Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 截图及;比较图像_C# - Fatal编程技术网

C# 截图及;比较图像

C# 截图及;比较图像,c#,C#,我使用下面的这些函数来截取用户的屏幕,并将此屏幕截图与参考资料中的图像bmpTest进行比较。如果找到与bmpTest兼容的内容,则返回该内容的位置 我的问题是:使用的算法有点慢,大约需要10-15秒来解释图像并给出结果。你们中有人知道其他方法吗?同样的方法,但速度更快?也许有百分之几的相似性?我就是在网上找不到 private Bitmap Screenshot() { // this is where we will store a snapshot of

我使用下面的这些函数来截取用户的屏幕,并将此屏幕截图与参考资料中的图像bmpTest进行比较。如果找到与bmpTest兼容的内容,则返回该内容的位置

我的问题是:使用的算法有点慢,大约需要10-15秒来解释图像并给出结果。你们中有人知道其他方法吗?同样的方法,但速度更快?也许有百分之几的相似性?我就是在网上找不到

      private Bitmap Screenshot()
    {
        // this is where we will store a snapshot of the screen
        Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

        // creates a graphics object so we can draw the screen in the bitmap (bmpScreenshot)
        Graphics g = Graphics.FromImage(bmpScreenshot);

        // copy from screen into the bitmap we created
        g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);

        //g.CopyFromScreen(205, 179, 660, 241, new Size(455, 62));

       // return the screenshot
        return bmpScreenshot;
    }


    private bool FindBitmap(Bitmap bmpNeedle, Bitmap bmpHaystack, out Point location)
    {
        for (int outerX = 0; outerX < bmpHaystack.Width - bmpNeedle.Width; outerX++)
        {
            for (int outerY = 0; outerY < bmpHaystack.Height - bmpNeedle.Height; outerY++)
            {
                for (int innerX = 0; innerX < bmpNeedle.Width; innerX++)
                {
                    for (int innerY = 0; innerY < bmpNeedle.Height; innerY++)
                    {
                        Color cNeedle = bmpNeedle.GetPixel(innerX, innerY);
                        Color cHaystack = bmpHaystack.GetPixel(innerX + outerX, innerY + outerY);

                        if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B)
                        {
                            goto notFound;
                        }
                    }
                }
                location = new Point(outerX, outerY);
                return true;
            notFound:
                continue;
            }
        }
        location = Point.Empty;
        return false;
    }



    private void button8_Click_1(object sender, EventArgs e)
    {
        // takes a snapshot of the screen
        Bitmap bmpScreenshot = Screenshot();

        // find the login button and check if it exists
        Point location;
        bool success = FindBitmap(Properties.Resources.bmpTest, bmpScreenshot, out location);

        // check if it found the bitmap
        if (success == false)
        {
            MessageBox.Show("Not Found");
            return;
        }
        else
        {
            MessageBox.Show("Image Found");
        }            
    }
private位图屏幕截图()
{
//这是我们将存储屏幕快照的位置
位图bmpScreenshot=新位图(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);
//创建图形对象,以便我们可以在位图中绘制屏幕(bmpScreenshot)
Graphics g=Graphics.FromImage(bmpScreenshot);
//从屏幕复制到我们创建的位图中
g、 CopyFromScreen(0,0,0,0,Screen.PrimaryScreen.Bounds.Size);
//g、 CopyFromScreen(205179660241,新尺寸(45562));
//返回屏幕截图
返回bmpScreenshot;
}
私有布尔FindBitmap(位图bmpFeedle、位图bmpAsystack、输出点位置)
{
对于(int-outerX=0;outerX
发生这种情况是因为对位图对象的每次“GetPixel”调用都会执行一个锁,这会导致性能问题

您可以通过自己处理锁来获得更好的性能,因此您可以一次锁定整个位图(在您的情况下是两个位图),然后遍历其像素

发生这种情况是因为对位图对象的每个“GetPixel”调用都会执行一个锁,这会导致性能问题

您可以通过自己处理锁来获得更好的性能,因此您可以一次锁定整个位图(在您的情况下是两个位图),然后遍历其像素


这种分析应该使用锁位,而不是Get-SetPixel。例如,请参阅。不使用Get SetPixel,而应使用锁位进行此类分析。有关示例,请参见。