I am trying to scan a .bmp file pixel by pixel to find the relevant RGB value of each of them and assign the result to a TPanel.Color.
The following is the code I am working on
procedure TForm4.FormCreate(Sender: TObject);
begin
FBmp := TBitmap.Create;
FBmp.PixelFormat := pf24bit;
FBmp.LoadFromFile('xxxxx\screen.bmp');
end;
function TForm4.FindPixelColour(const aX, aY: integer): PRGBTriple;
var
aPixel: PRGBTriple;
begin
aPixel := FBmp.ScanLine[aY];
Inc(aPixel, aX);
Result := aPixel;
end;
procedure TForm4.UpdatePanel(const aPixel: PRGBTriple);
var
Conversion: integer;
begin
PnlColour.Color := RGB(aPixel.rgbtRed, aPixel.rgbtGreen, aPixel.rgbtBlue);
Conversion := aPixel.rgbtRed;
label1.Caption := 'Red: ' + IntToStr(Conversion);
Conversion := aPixel.rgbtGreen;
label2.Caption := 'Green: ' + IntToStr(Conversion);
Conversion := aPixel.rgbtBlue;
label3.Caption := 'Blue: ' + IntToStr(Conversion);
end;
procedure TForm4.FormDestroy(Sender: TObject);
begin
fbmp.Free;
end;
When the program is in execution, the results from FindPixelColour seem to be inconsistent. Sometimes the RGB conversion shows the correct colors in PnlColour.Color but others don't.
I am sure the .bmp file is 24-bit but I also tried to change it into 36-bit and amending FBmp.PixelFormat accordingly but still no joy.
Where am I doing wrong? Is it the way I scan the .bmp or is it the way I assign the colors retrieved by TForm4.FindPixelColour to PnlColour.Color?
I have found the solution taking inspiration from this answer
Here is the correct code with improvements
Key changes are removing
Inc(aPixel, aX);fromTForm4.FindPixelColourand a different way to find the color channels inTForm4.UpdatePanel