Reputation: 1
public bool FindImageOnScreen(Bitmap searchFor, Bitmap searchIn)
{
for (int x = 0; x < searchIn.Width; x++)
{
for (int y = 0; y < searchIn.Width; y++)
{
Log($"X: {x}, Y: {y}");
if (y != searchIn.Height && x != searchIn.Width)
{
// First Check
if (searchIn.GetPixel(x, y) == searchFor.GetPixel(0, 0))
{
if (searchIn.GetPixel(x + (searchFor.Width / 2), y + (searchFor.Height / 2)) == searchFor.GetPixel(searchFor.Width / 2, searchFor.Height / 2))
{
return true;
}
}
}
else
{
break;
}
if (y == searchIn.Height && x == searchIn.Width)
{
return false;
}
}
}
return true;
}
Above is my script to locate an image on the screen but for some reason it takes minutes to do this, how can i speed this up?
It Worked But it Was Too Slow For me As I need it to work in an Instant
Upvotes: 0
Views: 70
Reputation: 899
your bool return value should be initialised with false and should be set to true only when you have your criteria met ( for what is see you are using probabilities)
hence your test:
if (y != searchIn.Height && x != searchIn.Width)
is useless as it is already handled by For conditions+initialised false.
you can use (learn to use) diagnostic tool embedded in visual studio. it can help to find the bottleneck. you could iterate until a criteria based on the dimension of the image search ( no way to check last column if you know image has not yet started. What is Log() doing ? if you write in file you have your answer.
Also if you increased y based on Height (and not on width) it could definitely help...
for (int x = 0; x < searchIn.Width; x++)
{
for (int y = 0; y < searchIn.Width; y++) // 2nd .Width: joke or typo?
{
Upvotes: 1