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

Author Topic: how do I use sf::Image::LoadFromMemory correctly?  (Read 5110 times)

0 Members and 1 Guest are viewing this topic.

Ankou

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
how do I use sf::Image::LoadFromMemory correctly?
« 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?

Avency

  • Full Member
  • ***
  • Posts: 113
    • View Profile
how do I use sf::Image::LoadFromMemory correctly?
« Reply #1 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?

Ankou

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
how do I use sf::Image::LoadFromMemory correctly?
« Reply #2 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
how do I use sf::Image::LoadFromMemory correctly?
« Reply #3 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.).
Laurent Gomila - SFML developer