Carregar imagem de uma URL
29, setembro, 2009
Pessoal, este é um exemplo de como carregar uma imagem de um site qualquer. Nessa aplicação utilizei a biblioteca GIFImg e o componente idHttp do pacote Indy. No form de exemplo tem os seguintes componentes:
Button1: TButton;
imgLogoEmbarcadero: TImage;
IdHTTP1: TIdHTTP;
No evento onclick do botão:
var
ImageMem : TMemoryStream;
ImageGif: TGIFImage;
begin
ImageMem := TMemoryStream.Create;
ImageGif := TGIFImage.Create;
try
try
IdHTTP1.Get('http://www.embarcadero.com/images/logo_new.gif', ImageMem);
except on e: EIdHTTPProtocolException do
begin
if e.ErrorCode = 404 then // código de página não encontrada
begin
// Não achou!
Exit;
end;
end;
end;
ImageMem.Position := 0;
ImageGif.LoadFromStream(ImageMem);
imgLogoEmbarcadero.Picture.Assign(ImageGif);
finally
ImageGif.Free;
ImageMem.Free;
end;
end;