Delphi detecting image type in TImageControl

  

I am trying to detect the image type loaded in FMX’s TImageControl. I am using Delphi 10.3 Rio.
My code is as follows:
function DetectImage(BM: TBitmap): string;
var
MS: TMemoryStream;
FirstBytes: AnsiString;
begin
MS := TMemoryStream.Create;
try
BM.SaveToStream(MS);
SetLength(FirstBytes, 8);
MS.Read(FirstBytes[1], 8);
if Copy(FirstBytes, 1, 2) = ‘BM’ then
begin
Result := ‘bmp’;
end
else if FirstBytes = #137’PNG’#13#10#26#10 then
begin
Result := ‘png’;
end
else if Copy(FirstBytes, 1, 3) = ‘GIF’ then
begin
Result := ‘gif’;
end
else if Copy(FirstBytes, 1, 2) = #$FF#$D8 then
begin
Result := ‘jpg’;
end
else
Result := ‘?’;

finally
MS.Free;
end;
end;

procedure TfrmMain.imgTeamAChange(Sender: TObject);
begin
ShowMessage(DetectImage(imgTeamA.Bitmap)) ;
end;

So, when I click on the TImageControl to change it, I always get ‘?’ as a result.
How do I make this working?

Comments are closed.