在运行时需要显示多个 JPG 或 GIF。然而,这些图像的大小差异很大,因此我使用两个 TImage,例如:ImGIF 和 ImGIFsmall,每个都在自己的 TPanel 上。根据加载图像的实际大小,代码将使用 ImGIF 或 ImGIFsmall。 我正在使用的代码是这样的:
var
pict : TPicture;
F_Small : boolean;
FName : string;
begin
F_Small := false;
pict := TPicture.Create;
try
pict.LoadFromFile(FName); // 1st load
if (pict.Width < 400) AND (Pict.Height < 400) then F_Small := true;
finally
pict.Free;
end;
if not F_Small then begin // big GIF - use big TIMage
Try
if FileExists(FName) then begin
With Form1 do begin
imGIF.Picture.LoadFromFile(FName); // 2nd load
(imGIF.Picture.Graphic as TGIFImage).Animate := true;
pnlGif.Visible := true;
imGIF.Visible := true;
pnlGifsmall.Visible := false;
imGIFsmall.Visible := false;
end;
end;
Except
.. // error message
End;
end else begin // small GIF - use small TIMage
... // same for imGIFsmall
end;
end;
如您所见,我两次访问磁盘上的 *.GIF 文件,可能会花费不必要的运行时间。我没有找到一种方法来使用打开的 Tpicture(获取其大小所需的)并将其放入 TImage 中。我所有的搜索结果都通过 LoadFromFile() 方法返回。
您只需将打开的
Assign()
TPicture
移动到所需的 TImage.Picture
即可释放它。