K Y Iyer
K Y Iyer

Reputation: 325

C++ Builder TImage Load From URL

Am having trouble figuring out how to load an image from a URL into a TImage control.

Broadly, this is what I need to do

   TMemoryStream *str = new TMemoryStream;
   http->Get("http://myurl.mydomain.com/myimage.jpg", str);

The problem seems to be that str has no space allocated to it. And I get an "Invalid IO handle error".

I cannot find out the size of the image before hand.

How should I do this, please? I tried to allocate 10MB of space to TMemoryStream by creating a dummy file and loading that file into the TMemoryStream but that also throws an "Invalid IO Handler" error.

Using C++ Builder on RAD Studio XE7 to develop iOS and Android apps.

Thanks a lot

Iyer

Upvotes: 0

Views: 524

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 597941

The problem is not with the code you have shown. TMemoryStream grows dynamically as data is written into it, and TIdHTTP::Get() will write whatever data the server actually sends. The code syntax you have shown is fine.

If you are getting an "IOHandler value is not valid" error from Get(), it means an HTTPS url is being requested but a TIdSSLIOHandlerSocketBase-derived component (such as TIdSSLIOHandlerSocketOpenSSL) is not assigned to the TIdHTTP::IOHandler property to handle SSL/TLS encryption. Your code is requesting an HTTP url, so the server is likely sending a redirect response to tell TIdHTTP to request an HTTPS url instead (check the TIdHTTP::OnRedirect event to verify). You need to have an SSL IOHandler assigned in order to handle HTTPS urls.

Upvotes: 0

Related Questions