Retrieve Image via Stream

Hello,


thanks for this nice toolset. In the past I was using the Overbyte ICS components but want to switch to Sparkle. At present I am stuck with the following:

I try to retrieve an image from the web (e.g. http://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Ferrybuilding1.jpg/250px-Ferrybuilding1.jpg) and assign it to a TImage on a FireMonkey form.

My attempts via GetContentsasStream have been unsuccessful so far. Would you have some guidance or sample to show how this is done? 

Many thanks in advance
Gernot

What is the code you tried?

Be aware that the stream returned by ContentAsStream does not provide a value for Size property - because it retrieves data on demand, it's a forward-only stream that you should use to retrieve chunked data, for example. You must rely only on Read method of that stream. You can also get the content as TBytes, and then transfer those bytes to the image.

A code like this works, for example:


    S := TBytesStream.Create(Resp.ContentAsBytes);
    try
      Image1.Bitmap.LoadFromStream(S);
    finally
      S.Free;
    end;

Thanks Wagner,


in the meantime I solved it based on the FillResponseBinary from the sample.

    bt := Resp.ContentAsBytes;
    ms := TMemoryStream.Create;
    ms.WriteBuffer(bt,Length(bt));
    bmp := TBitmap.CreateFromStream(ms);

Kind regards
Gernot