During run time several JPGs, or GIFs, need to be diplayed. However, the size of these images differs greatly and therefore I use two TImages, e.g.: ImGIF and ImGIFsmall, each on their own TPanel. Based on the actual size of the loaded image the code will use ImGIF or ImGIFsmall. The code that I am using is this:
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;
As you see, I am accessing the *.GIF file on disk twice, probably costing unnecessary run time. I didn't find a way to use the open Tpicture (required for getting its size) and put that into the TImage. All my search results come back with the LoadFromFile() method.
You can simply Assign()
the opened TPicture
to the desired TImage.Picture
before freeing it, eg:
var
pict: TPicture;
img: TImage;
F_Small: Boolean;
...
begin
...
pict := TPicture.Create;
try
pict.LoadFromFile(...);
F_Small := (pict.Width < 400) and (Pict.Height < 400);
if F_Small then
img := Form1.imGIFsmall
else
img := Form1.imGIF;
img.Picture.Assign(pict);
finally
pict.Free;
end;
if img.Picture.Graphic is TGIFImage then
TGIFImage(img.Picture.Graphic).Animate := True;
Form1.pnlGif.Visible := not F_Small;
Form1.imGIF.Visible := not F_Small;
Form1.pnlGifsmall.Visible := F_Small;
Form1.imGIFsmall.Visible := F_Small;
...
end;