SFML community forums
Help => Graphics => Topic started by: Ankou on February 28, 2008, 05:35:14 pm
-
I tried the following
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?
-
Do
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?
-
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?
-
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.).