tags:

views:

719

answers:

3

Hello, I am looking for any code samples that show how to pull images from URL into Delphi TImage component.

Thanks,

+10  A: 

With help of TMemoryStream and Indy component.

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;
Mohammed Nasman
You could even do without the TGifImage and do img1.Picture.LoadFromStream(MS);
Lars Truijens
**Warning:** If there is an exception in `TIdHTTP.Get`, then this code will free the uninitialized `GIF` variable. Do not use variables in a "finally" section if you haven't initialized them *before* entering the corresponding "try" section.
Rob Kennedy
No, Lars, you can't do that. `TPicture.LoadFromStream` is protected, not public. Besides, it just calls `Bitmap.LoadFromStream`, so it won't know how to load GIF data anyway.
Rob Kennedy
Thanks Rob, I did it in hurry specially the Gif side, I was looking to store the Stream directly into TImage but recognized it's not available.
Mohammed Nasman
TWO NEGATIVES VOTES!!!!!, could you please explain why you vote down ????
Mohammed Nasman
Mohammed, The code works with GIF as you posted. I appreciated it. I was not able to use with JPEGs (I use D2009).I posted my code above. any suggestions would be appreciatedThanks, Chris aka Greener
Greener
I have just found my typo in the code. It worked with JPEG. I do not get why negative votes. THANKS AGAIN !!!
Greener
+1  A: 

Code Worked for JPEG as well.

Greener
Add "uses JPEG ", I made my example with GIF unit, you need to use Jpeg one.
Mohammed Nasman
Greener, you have not used the right `Get` method. If you want it to fill a stream, you need to remember to pass the stream as a parameter. The one-argument version of `Get` returns the resource contents as a string in the function's return value.
Rob Kennedy
Yes Rob, I found my mistake. Thanks again to everyone for help.
Greener
You should edit your first post or add comments to relevant answers instead.
Ertugrul Tamer Kara
Sorry, I did not know. Next time I will be more careful...
Greener
A: 

Tank you a lot, very usefull code, i was getting crazy assigning directly the "Stream" to "image1.picture"

but i have to use the code to retrive this file: http://www.clickufficio.it/5308.image?Catalog=HOPE%5FClick&ID=10613&Type=Normal&Name=1

it is not a proper file, but if you past it in the browser you get a picture (it has no html code)

but in Delphi it raises an exception. It seems there is no redirect ether.

How can i get the real path of the file?

ty you in advance Antonio