在另一个位图内查找位图的位置



我正在编写一个程序,该程序将屏幕屏幕屏幕屏幕,转换为位图,然后几乎使用bitmap" key"搜索屏幕记录的位图。它将位图"键"与整个图片进行比较,找到它们匹配的位置,将光标移至位置,然后单击鼠标。我在bmpLogin为null时遇到麻烦。我在Mopar/Mopar/Resources/bmpLogin.bmp中有我的位图键。我似乎找不到此图像进行比较。这是相关代码。

开始按钮:

    private void startBotButton(object sender, EventArgs e)
    {
        //Screenshot
        //This is where we will be writing the loop for the bot
        Bitmap bmpScreenshot = Screenshot();
        //Set the background of the form the screenshot that was taken
        this.BackgroundImage = bmpScreenshot;
        //Find the node and check if it exists within the screenshot
        Point location;
        bool success = FindBitmap(Properties.Resources.bmpLogin, bmpScreenshot, out location);
        //Check if it found the node in the bitmap
        if (success == false)
        {
            MessageBox.Show("Couldn't find the node");
            return;
        }
        //Move the mouse to the node
        Cursor.Position = location;
        //click
        MouseClick();
    }

屏幕截图:

    //Screenshot the screen
    private Bitmap Screenshot()
    {
        //This is where we will store the screenshot
        Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        //Create a graphics object so we can draw the screen in the bitmap
        Graphics g = Graphics.FromImage(bmpScreenshot);
        //Copy from screen to bitmap
        g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
        //Return the screenshot
        return bmpScreenshot;
    }

搜索位图:

    /// <summary>
    /// Find the location of a bitmap within another bitmap and return if it was successfully found
    /// </summary>
    /// <param name="bmpNeedle">The image we want to find</param>
    /// <param name="bmpHaystack">Where we want to search for the image</param>
    /// <param name="location">Where we found the image</param>
    /// <returns>If the bmpNeedle was found successfully</returns>
    private bool FindBitmap(Bitmap bmpNeedle, Bitmap bmpHaystack, out Point location)
    {
        if (bmpNeedle == null || bmpHaystack == null)
        {
            location = new Point();
            return false;
        }
        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;
    }

担心我的代表太低而无法发表评论。因此,对不完整的答案表示歉意

您不足以更明确地回答(我们需要查看您的资源如何包含在项目中)...但是,这是有关您问题的各种rootcause的一个很好的答案:https://social.msdn.microsoft.com/forums/vstudio/en-us/a4ace5de-9376-44d0-a453-1f1c39c47a86/where-did-the-propertiesreserces-go?forum=csharpgeneral = csharpgeneral

为了提高可读性(并防止goto),我将考虑将内部的两个循环移动到功能中。

最后(可能是最重要的是),对于性能,您始终希望在外循环上迭代y,而在内部循环上进行x。您应该注意到任何显着尺寸的图像的大幅加速。

最新更新