同时在2个背景工作者中查找位图 - 没有克隆

时间:2017-03-16 09:09:55

标签: c# bitmap

我有2个背景工作者和2个激活它们的按钮。我还有FindBitmap的功能:

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 - 8, outerY - 32);
                    return true;
                    notFound:
                    continue;
                }
            }
            location = Point.Empty;
            return false;
        }

当我添加第二个背景工作者并使用内部函数来查找Findbitmap时出现错误: 类型&#39; System.InvalidOperationException&#39;的例外情况发生在System.Drawing.dll中但未在用户代码中处理

其他信息:该对象目前正在其他地方使用。

我知道我不能同时在2个后台工作者中使用System.Drawing。 我还能做些什么来避免这个问题?

使用Sscreenshoot:

   private Bitmap Screenshot()
        {
            RECT rt = new RECT();
            GetWindowRect(hwnd1, out rt);
            int width = rt.Right - rt.Left;
            int height = rt.Bottom - rt.Top;

            Bitmap bmp = new Bitmap(width, height);
            Graphics screenG = Graphics.FromImage(bmp);


            screenG.CopyFromScreen(rt.Left, rt.Top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);

            bmp.Save(@"D:\printscreennnnnnnnnnn.jpg", ImageFormat.Jpeg);
            return bmp;
            //screenG.ReleaseHdc();





        }

我尝试使用RealeseHdc,但它没有帮助。 我也试着创建第二个函数:`Findbitmap1() 并把它带入第二背景工作者,但它没有帮助 我要添加我的屏幕:Form1.resx。和屏幕显示问题的外观:problem.jpg

我搜索了解决方案,但有点难。我有和想法创建一个新的Form2.cs并采取新的后台工作者并复制一切,也许这将工作?

0 个答案:

没有答案