{ Ce code est compatible avec les nouvelles lib de Delphi2009
et utilise Indy10 }
Uses
Windows, SysUtils, Dialogs, classes, Graphics,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP,
ExtCtrls, jpeg
{$ifdef ver200}, pngimage, gifimg{$endif};
Function LoadImageFromUrl(Const aUrl: String; Const aImage: TImage): boolean;
Implementation
Function GetGraphicClass(Const aFileName: String): TGraphicClass;
Var aExt: String;
Begin
Result := Nil;
aExt := LowerCase(ExtractFileExt(aFileName));
If aExt = '.ico' Then
Result := TIcon
Else
If aExt = '.bmp' Then
Result := TBitmap
Else
If (aExt = '.jpg') Or (aExt = '.jpeg') Then
Result := TJPEGImage
{$ifdef ver200}
Else
If aExt = '.gif' Then
Result := TGIFImage
Else
If aExt = '.png' Then
Result := TPNGImage
{$endif};
End;
{ Charge une image depuis le net et l'affiche dans un TImage
sans passer par le HDD }
Function LoadImageFromUrl(Const aUrl: String; Const aImage: TImage): boolean;
Var
NewGraphic : TGraphic;
GraphicClass : TGraphicClass;
aStream : TMemoryStream;
Begin
Result := assigned(aImage);
If Result Then
Begin
GraphicClass := GetGraphicClass(aUrl);
If GraphicClass = Nil Then
Begin
Result := False;
Exit;
End;
NewGraphic := GraphicClass.Create;
aStream := TMemoryStream.Create;
With TIdHTTP.Create(Nil) Do
Try
Try
Get(aUrl, aStream);
aStream.Seek(0, soFromBeginning);
NewGraphic.LoadFromStream(aStream);
Except
Result := False;
End;
Finally
aImage.Picture.Graphic := NewGraphic;
NewGraphic.Free;
aStream.Free;
Free;
End;
End;
End;
End.
{ Utilisation }
LoadImageFromUrl('http://www.delphifr.com/gdi/c/45850.cs.jpg', image1);