SFML community forums

Help => Graphics => Topic started by: Ankou on February 28, 2008, 05:35:14 pm

Title: how do I use sf::Image::LoadFromMemory correctly?
Post by: Ankou on February 28, 2008, 05:35:14 pm
I tried the following
Code: [Select]

  std::ifstream file("C:\\Main\\Tools\\RMXP\\RTP\\Graphics\\Tilesets\\001-Grassland01.png");
  int size = FileSize("C:\\Main\\Tools\\RMXP\\RTP\\Graphics\\Tilesets\\001-Grassland01.png");
  char* image = new char[size];
  file.get(image, size);
  sf::Image img;
  img.LoadFromMemory(image, size);

But it doesn't work (Image not of any known type or corrupt), the file is okay, so what do I have to do to load an image from Memory?
Title: how do I use sf::Image::LoadFromMemory correctly?
Post by: Avency on February 28, 2008, 06:52:53 pm
Do

Code: [Select]

    char* memory;
   
    std::ifstream myfile("image.png", std::ios::binary | std::ios::ate);
   
    std::ifstream::pos_type size;
   
    size = myfile.tellg();
   
    memory = new char[size];
   
    myfile.seekg(0, std::ios::beg);
    myfile.read(memory, size);
    myfile.close();
   
    sf::Image Image;
    Image.LoadFromMemory(memory,size);

    delete[] memory;

It should work unless your image is corrupted. But why won't you use sf::Image::LoadFromFile?
Title: how do I use sf::Image::LoadFromMemory correctly?
Post by: Ankou on February 28, 2008, 07:01:31 pm
I'm using LoadFromMemory, because I want to put more then one image in a file, this code was just a test, if I can get it working.
And your code works, but what's the keydifference between your code and mine?
Title: how do I use sf::Image::LoadFromMemory correctly?
Post by: Laurent on February 29, 2008, 02:15:43 am
Basically, your code doesn't use the stream as binary (std::ios::binary flag and read function). It uses the default behaviour of streams, which is text-based (special characters are interpreted, etc.).