2009-08-04 14 views

Odpowiedz

28

Z pomocą komponentu TMemoryStream i Indy.

uses 
    GIFImg; 

procedure TForm1.btn1Click(Sender: TObject); 
var 
    MS : TMemoryStream; 
    GIf: TGIFImage; 
begin 
    MS := TMemoryStream.Create; 
    GIf := TGIFImage.Create; 
    try 
    IdHTTP1.get('http://www.google.com/intl/en_ALL/images/logo.gif',MS); 
    Ms.Seek(0,soFromBeginning);  
    Gif.LoadFromStream(MS); 
    img1.Picture.Assign(GIF); 

    finally 
    FreeAndNil(GIF); 
    FreeAndNil(MS); 
    end; 
end; 
+0

Można nawet zrobić bez TGifImage i zrobić img1.Picture.LoadFromStream (MS); –

+1

** Uwaga: ** Jeśli jest wyjątek w 'TIdHTTP.Get', ten kod zwolni niezainicjowaną zmienną' GIF'. Nie używaj zmiennych w sekcji "finally", jeśli nie zainicjowałeś ich * przed * wprowadzeniem odpowiedniej sekcji "try". –

+1

Nie, Lars, nie możesz tego zrobić. 'TPicture.LoadFromStream' jest chroniony, a nie publiczny. Poza tym po prostu wywołuje 'Bitmap.LoadFromStream', więc i tak nie będzie wiedział, jak załadować dane GIF. –

3

Kod Obowiązuje również dla JPEG.

+0

Dodaj "wykorzystuje JPEG", zrobiłem mój przykład z jednostką GIF, musisz użyć Jpeg. –

+0

Bardziej ekologiczne, nie użyłeś właściwej metody "Get". Jeśli chcesz, aby wypełniał strumień, musisz pamiętać, aby przekazać strumień jako parametr. Jednokondensacyjna wersja 'Get' zwraca zawartość zasobów jako ciąg znaków w wartości zwracanej przez funkcję. –

+0

Tak Rob, Znalazłem swój błąd. Jeszcze raz dziękuję wszystkim za pomoc. – Greener

0

Dla this project Użyłem komponentów Indy (takich jak pierwsza odpowiedź), ale używając kodu wewnątrz wątku. Przydatne do pobierania dużych obrazów lub dużej liczby zdjęć. Możesz zobaczyć pełne wyjaśnienie projektu w linku (jest w języku hiszpańskim, ale możesz użyć automatycznego tłumaczenia).

W tym przypadku używam go do pobierania wszystkich zdjęć from this page.
Tutaj używam innego komponentu IdSSL:TIdSSLIOHandlerSocket, niezbędnego do uzyskania dostępu do adresu URL https; Jeśli musisz uzyskać dostęp do http, nie potrzebujesz go.

Kodeks TDownImageThread jest (dodane komentarze języku angielskim):

{: Clase para descargar una imagen y almacenarla en disco.} 
    {: Class to download image and save to disk} 
    TDownImageThread = class(TThread) 
    private 
    FURLImage: string; 
    FPathImage: string; 
    FFileNameImage: string; 
    // Internas 
    ImageName: string; 
    PathURL: string; 
    // Componente 
    idH:TidHTTP; 
    IdSSL:TIdSSLIOHandlerSocket; 
    public 
    // redefinir métodos // redefine methods 
    constructor Create(AURL:string; AOutPathImages:string); 
    destructor Destroy; override; 
    procedure Execute; override; 
    {: URL de la imagen a descargar. // URL to download} 
    property URLImage:string read FURLImage write FURLImage; 
    {: Path de disco local donde voy a almacenar la imagen.} 
    {: Local path to save the images} 
    property PathImage:string read FPathImage; 
    {: Nombre completa (path+Nombre) de la imagen almacenada en disco local} 
    {: complete name (path+name) of loval image} 
    property FileNameImage:string read FFileNameImage; 
    end; 



.... 


{ TDownImageThread } 
constructor TDownImageThread.Create(AURL, AOutPathImages: string); 
var 
    URI:TidURI; 
begin 

    // crear el thread suspendido // Create suspended thread 
    inherited Create(True); 
    // Parámetros: URL y dir de salida // Params URL and output dir. 
    Self.FURLImage := AURL; 
    Self.FPathImage := AOutPathImages; 
    // Crear con URL // create with URL 
    URI := TidURI.Create(AURL); 
    try 
    ImageName := URI.Document; 
    PathURL := URI.Path; 
    finally 
    URI.Free; 
    end; 
end; 

destructor TDownImageThread.Destroy; 
begin 
    inherited; 
end; 

//: recupara la imagen y la guarda en disco 
procedure TDownImageThread.Execute(); 
var 
    Stream:TFileStream; 
    IdH:TidHTTP; 
    IdSSL:TIdSSLIOHandlerSocket; 
    path:string; 
    dir:string; 
begin 
    // Directorio de salida // output directory 
    dir := AnsiReplaceText(PathURL, '/', STR_EMPTY); 
    // Nombre vacío // empty name 
    if (ImageName = STR_EMPTY) then begin 
    Exit; 
    end; 
    // Path de salida  // output path 
    path := IncludeTrailingBackslash(IncludeTrailingBackslash(PathImage) + 
      dir) + ImageName; 
    // Crearlo por si no existe // create it if not exist 
    ForceDirectories(ExtractFilePath(path)); 
    try 
    // Stream para la imagen // Stream for the image 
    Stream := TFileStream.Create(path, fmCreate); 
    try 
     // Crear componente para acceder /// Create the component in runtime 
     IdH := TidHttp.Create(nil); 
     IdH.ReadTimeout := 30000; 
     // necessary to use HTTPS 
     IdSSL := TIdSSLIOHandlerSocket.Create(nil); 
     IdH.IOHandler := IdSSL; 
     IdSSL.SSLOptions.Method := sslvTLSv1; 
     IdSSL.SSLOptions.Mode := sslmUnassigned; 
     idH.HandleRedirects := True; 
     IdH.RedirectMaximum := 3; 

     // proteccion 
     try 
     // Obtener la imagen // get the image 
     IdH.Get(Trim(FURLImage), Stream); 
     except 
     // Error al descargar la imagen 
     //.. Volcarlo al log 
     end; 
    finally 
     // Liberar // Free component 
     idH.Free; 
     // IdSSL.Free; 
     Stream.Free; 
    end; 
    // Path de salida  // output path 
    FFileNameImage := path; 
    except 
    // error al crear el fichero // error on create file 
    //... Log 
    end; 
end; 

Aby go użyć, wywołanie jest podobny do tego:

// Crear un nuevo thread para descargar la imagen 
// Create a new thread LINK+output path 
th := TDownImageThread.Create(mmLinks.Lines[i], pathImages); 
// Procedimiento de retorno al finalizar 
// procedure to return on thread finalize 
th.OnTerminate := TerminateThread; 
th.Resume; 
0

Lepsze wykorzystanie tej funkcji do pobierania:

function DownloadFile(Url, DestFile: string): Boolean; 
begin 
    try 
    Result := UrlDownloadToFile(nil, PChar(Url), PChar(DestFile), 0, nil) = 0; 
    except 
    Result := False; 
    end; 
end; 
Powiązane problemy