I have 2 backgroundworkers and 2 button that activate them. Also i have a function to 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;
}
When I added a second backgroundworker and I used inside function to Findbitmap i have an error : An exception of type 'System.InvalidOperationException' occurred in System.Drawing.dll but was not handled in user code
Additional information: The object is currently in use elsewhere.
I know I can't use System.Drawing in 2 backgroundworkers in the same time. What else i can do to avoid this problem?
To take a Sscreenshoot i use :
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();
}
I try to use RealeseHdc
but it don't help.
Also i try to just create a 2nd function : `Findbitmap1 ()
and take it into 2nd background worker but its the same it don't help
I gonna add Screen of my:Form1.resx . and Screen exacly where is problem look : problem.jpg
I searched for solution but its kinda hard. I have and idea to create a new Form2.cs and take new background worker and copy everything and maybe this gonna work?