I currently take screenshots of an area in a loop to then search for 4 pixels in it. Those pixels do have the same color - red or $001300FF . The variables that are used are defined and initialized in the OnCreate event:
//The variables for the area:
ScanL := 500; // Left
ScanR := 800; // Right
ScanT := 180; // Top
ScanB := 400; // Bottom
screenshot: TBitMap;
canvas : TCanvas;
To take the screenshots I use the following function:
procedure TFormMain.GetSCREENSHOT(var a: TBitMap);
var
Locked: Boolean;
begin
Locked := Canvas.TryLock;
try
screenshot.Canvas.CopyRect(screenshot.Canvas.ClipRect, Canvas, Rect(ScanL, ScanT, ScanR, ScanB));
finally
if Locked then
Canvas.Unlock;
end;
end;
The variable "screenshot : TBitMap", globally defined gets passed to the GetSCREENSHOT-function. To search for those 4 pixels I just did what a newbie would do:
function TFormMain.findImage : Boolean;
var
x,y : Integer;
begin
Result := false;
for x := 0 to screenshot.Width-10 do
begin
for y := 0 to screenshot.Height-10 do
begin
if screenshot.Canvas.Pixels[x,y] = $001300FF then
begin
if screenshot.Canvas.Pixels[x,y+1] = $001300FF then
if screenshot.Canvas.Pixels[x,y+2] = $001300FF then
if screenshot.Canvas.Pixels[x,y+3] = $001300FF then
begin
FoundPixelX := ScanL + x;
FoundPixelY := ScanT + Y;
Result := True;
Exit;
end;
end;
end;
end;
end;
Because it performed so bad, I measured how long it takes to run the function:
QueryPerformanceFrequency(freq);
QueryPerformanceCounter(startTime);
findImage;
QueryPerformanceCounter(endTime);
ShowMessage('the function needs about ' + IntToStr((endTime - startTime) * 1000 div freq) + 'ms');
and it takes 108ms! That's crazy. I don't know why and I hope you can help me how to improve it! I thought that it maybe has something to do with the access of the .Pixels property ?!
To compare: getSCREENSHOT takes less than 1ms.