Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Load image from the internet  (Read 6495 times)

0 Members and 1 Guest are viewing this topic.

WiltedChameleon

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Load image from the internet
« on: June 05, 2012, 01:37:28 am »
Is there any trivial way, perhaps using the HTTP or FTP sub-modules of the networking module, to load an image into a texture or image without actually putting the image onto the local machine?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Load image from the internet
« Reply #1 on: June 05, 2012, 07:58:41 am »
Yes, you can download the image with sf::Http and then load it into a texture/image with the loadFromMemory function.
Laurent Gomila - SFML developer

WiltedChameleon

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: Load image from the internet
« Reply #2 on: June 05, 2012, 01:20:13 pm »
Sorry to bother you again

I've got this:
playerTexture.loadFromMemory(&avatarResponse.getBody(), avatarResponse.getBody().size());

But it says I have an invalid image. I think I'm doing it totally wrong but can't imagine a different way of doing it. Any chance you can see what I've done wrong?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Load image from the internet
« Reply #3 on: June 05, 2012, 01:23:24 pm »
Save the contents of "avatarResponse.getBody()" to a file, then download the image (with a regular internet browser), and compare both files to see what's different.
Laurent Gomila - SFML developer

WiltedChameleon

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: Load image from the internet
« Reply #4 on: June 05, 2012, 01:43:10 pm »
I've got very little experience with file I/O (I've only ever inputted and outputted basic text files) in C++, but I ended up with this:

        std::ofstream file;
        file.open("test.png", std::ios::binary, std::ios::out | std::ios::in | std::ios::trunc);

        file << avatarResponse.getBody();

        file.close();

And it crashes my program.

Am I using http::Response and http::Request right?

sf::Http::Request avatarRequest("/Internal/Images/Avatars/3.png");
sf::Http::Response avatarResponse = http.sendRequest(avatarRequest);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Load image from the internet
« Reply #5 on: June 05, 2012, 01:55:36 pm »
To write raw data, as opposed to formatted text, in an output stream, you must use the write member function instead of the << operator.

Then your code is incorrect: all std::ios flags must be combined with |, you mustn't split them into two separate arguments.

std::ios::out and std::ios::trunc are implicit for std::ofstream.
std::ios::in makes no sense for std::ofstream.

Try this:
std::string body = avatarResponse.getBody();
std::ofstream file;
file.open("test.png", std::ios::binary);
file.write(body.c_str(), body.size());
« Last Edit: June 05, 2012, 01:58:50 pm by Laurent »
Laurent Gomila - SFML developer

WiltedChameleon

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: Load image from the internet
« Reply #6 on: June 05, 2012, 02:01:35 pm »
Ah, thank you, especially for correcting my file code. I'll try remembering that, although most of my mistakes seem obvious and stupid now that I've been shown them.

Yeah, the image outputs perfectly, looks identical to the one found on the site.

Edit
When trying to load the image the error says
Failed to load image from memory. Reason : Image not of any known type or corrupt

Edit

Using c_str on the body when loading from memory works and the texture loads correctly. Thanks! :D
« Last Edit: June 05, 2012, 02:05:22 pm by WiltedChameleon »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Load image from the internet
« Reply #7 on: June 05, 2012, 02:07:04 pm »
Quote
Using c_str on the body when loading from memory works and the texture loads correctly.
Oh yeah, I missed that:
&avatarResponse.getBody()
This was actually an obvious mistake -- taking the adress of the string instead of its contents :P
Laurent Gomila - SFML developer

WiltedChameleon

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: Load image from the internet
« Reply #8 on: June 05, 2012, 02:10:00 pm »
Quote
Using c_str on the body when loading from memory works and the texture loads correctly.
Oh yeah, I missed that:
&avatarResponse.getBody()
This was actually an obvious mistake -- taking the adress of the string instead of its contents :P

All the ampersands and the asterisks are destroying my mind x3

Gerard Wensink

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Github
    • Email
Re: Load image from the internet
« Reply #9 on: April 01, 2018, 01:29:25 am »
You should use the .data()
Like this:
   sf::Texture tussen;

   if (    !tussen.loadFromMemory(   response.getBody().data(), response.getBody().size()  )     )

Gerard Wensink

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Github
    • Email
Re: Load image from the internet
« Reply #10 on: April 01, 2018, 01:30:38 am »
« Last Edit: April 01, 2018, 07:12:24 am by eXpl0it3r »