This is the code for LoadFromFile
:
procedure TCustomBitmap32.LoadFromFile(const FileName: string);
var
FileStream: TFileStream;
Header: TBmpHeader;
P: TPicture;
begin
FileStream := TFileStream.Create(Filename, fmOpenRead);
try
FileStream.ReadBuffer(Header, SizeOf(TBmpHeader));
// Check for Windows bitmap magic bytes...
if Header.bfType = $4D42 then
begin
// if it is, use our stream read method...
FileStream.Seek(-SizeOf(TBmpHeader), soFromCurrent);
LoadFromStream(FileStream);
Exit;
end
finally
FileStream.Free;
end;
// if we got here, use the fallback approach via TPicture...
P := TPicture.Create;
try
P.LoadFromFile(FileName);
Assign(P);
finally
P.Free;
end;
end;
As you can see, the code checks to see if the file is a Windows bitmap. If so then it loads it directly by calling LoadFromStream
.
If not then it loads into a TPicture
and then assigns to this instance.
The point is that LoadFromStream
only understands Windows bitmaps. It does not understand any other file format. And you are trying to load a PNG. So, there's no way to do what you need without using an intermediate object.
The solution for you is to:
- Instantiate a
TPNGImage
object. - Call
LoadFromStream
on that instance. - Call
Assign
on theTBitmap32
instance passing theTPNGImage
instance.