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

Author Topic: [Solved] Failed to load image from memory, no data provided  (Read 10112 times)

0 Members and 1 Guest are viewing this topic.

tyrbo321

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
[Solved] Failed to load image from memory, no data provided
« on: December 11, 2015, 07:17:37 pm »
I'm using SFML with C++ in VS2012. In debug console i have error "failed to load image from memory, no data provided".



My main.cpp

#include <iostream>
#include "Test.h"
#include "resource.h"
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

using namespace std;

sf::Image LoadImageFromResource(const std::string& name)
{
    HRSRC rsrcData = FindResource(NULL, name.c_str(), RT_RCDATA);

    DWORD rsrcDataSize = SizeofResource(NULL, rsrcData);

    HGLOBAL grsrcData = LoadResource(NULL, rsrcData);

    LPVOID firstByte = LockResource(grsrcData);


    sf::Image image;
    image.loadFromMemory(firstByte, rsrcDataSize);


    return image;
}

int main()
{      
    sf::Image SpriteSheetWalkRes = LoadImageFromResource("IDB_PNG1");
}


My Resource.rc

#define APSTUDIO_READONLY_SYMBOLS

#include "afxres.h"

#undef APSTUDIO_READONLY_SYMBOLS

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_RUS)
LANGUAGE LANG_RUSSIAN, SUBLANG_DEFAULT

#ifdef APSTUDIO_INVOKED

1 TEXTINCLUDE
BEGIN
    "resource.h\0"
END

2 TEXTINCLUDE
BEGIN
    "#include ""afxres.h""\r\n"
    "\0"
END

3 TEXTINCLUDE
BEGIN
    "\r\n"
    "\0"
END

#endif

IDB_PNG1            RCDATA           "F:\\(...)\\test_player.png"

#endif

#ifndef APSTUDIO_INVOKED

#endif

 


My resource.h

#define IDB_PNG1                        101

#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        102
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1001
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif
« Last Edit: December 13, 2015, 12:07:08 pm by tyrbo321 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Failed to load image from memory, no data provided
« Reply #1 on: December 11, 2015, 07:26:52 pm »
Since this is the direct result of the following statement failing, it means that either your size or your pointer is 0.

if (data && dataSize)

It's always a good advice to run your code in a debugger and/or printing the variables out, to verify their content.
« Last Edit: December 11, 2015, 07:28:43 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tyrbo321

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Failed to load image from memory, no data provided
« Reply #2 on: December 11, 2015, 07:52:33 pm »
Since this is the direct result of the following statement failing, it means that either your size or your pointer is 0.

if (data && dataSize)

It's always a good advice to run your code in a debugger and/or printing the variables out, to verify their content.
Also im trying use bitmap for load, like in original sample, and again this error, i think i have wrong code.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Failed to load image from memory, no data provided
« Reply #3 on: December 11, 2015, 08:05:34 pm »
As mentioned, start the debugger and check your variables. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tyrbo321

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Failed to load image from memory, no data provided
« Reply #4 on: December 11, 2015, 11:06:29 pm »
This method works for you, have you ever used it?
If you can, please, make project with that system, and add any resource (just image) on sfml, and give link me, it will be easier to understand the issue. Maybe i have wrong project settings.
« Last Edit: December 11, 2015, 11:08:10 pm by tyrbo321 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Failed to load image from memory, no data provided
« Reply #5 on: December 11, 2015, 11:27:40 pm »
loadFromMemory works fine if you pass it the correct data. As I said before either your rsrcDataSize or your firstByte is zero thus it doesn't work. The problem is in the resource loading part, i.e. your own code. Thus debug it and/or read the docs again on how to use it.
Personally I've never used the Windows resource system to load things, so no idea how it works.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3349
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Failed to load image from memory, no data provided
« Reply #6 on: December 12, 2015, 12:43:18 am »
Are you sure that
IDB_PNG1            RCDATA           "F:\\(...)\\test_player.png"
is correct?
Wouldn't this just take the literal path string as the raw data stored in the resource?

RCDATA
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

tyrbo321

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Failed to load image from memory, no data provided
« Reply #7 on: December 12, 2015, 03:18:26 am »
Crap... No resource problem and no file path problem, just dont want load any image from resources. I need working project to know issue.

Hapax

  • Hero Member
  • *****
  • Posts: 3349
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Failed to load image from memory, no data provided
« Reply #8 on: December 12, 2015, 05:21:58 am »
I don't fully understand what you mean. Did that help?

just dont want load any image from resources.
If your goal is to avoid having separate files, you can link them into your executable without using Microsoft's resources. A simple way is to convert the binary file into a header file - with an array of the data - that can be linked into your project and built into your executable. This has the added benefit that it also allows your project to be cross-platform.

If you would prefer to continue with Microsoft's resource manager, you may find what you need here:
ICON resource
BITMAP resource
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

tyrbo321

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Failed to load image from memory, no data provided
« Reply #9 on: December 12, 2015, 03:53:51 pm »
I don't fully understand what you mean. Did that help?

just dont want load any image from resources.
If your goal is to avoid having separate files, you can link them into your executable without using Microsoft's resources. A simple way is to convert the binary file into a header file - with an array of the data - that can be linked into your project and built into your executable. This has the added benefit that it also allows your project to be cross-platform.

If you would prefer to continue with Microsoft's resource manager, you may find what you need here:
ICON resource
BITMAP resource

Thx man, i read about converting the binary file into a header file, and i convert my png to .h now, but i cant find examples, and manuals how open it as image. I include .h now, ok, but as i try load it as image it does not work (not surprising). Maybe my question stupid, but how?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Failed to load image from memory, no data provided
« Reply #10 on: December 12, 2015, 04:05:03 pm »
Maybe if you provided a Short, Self Contained, Correct (Compilable), Example people would be able to help you better...
Right now my impression is that SFML works just fine (especially given that I've personally used these bits of the API with no trouble) and that the bug must be in your code.
Provide a complete example (see link above) and then we'll all have a much better base to talk about.

Hapax

  • Hero Member
  • *****
  • Posts: 3349
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Failed to load image from memory, no data provided
« Reply #11 on: December 12, 2015, 05:17:04 pm »
i convert my png to .h now, but i cant find examples, and manuals how open it as image. I include .h now, ok, but as i try load it as image it does not work (not surprising). Maybe my question stupid, but how?
There has been talks of this on the forum before but I can't remember where.

Here's a simple example of how to load an image file from a header into an sf::Image:
#include <SFML/Graphics.hpp>
#include "icon_png.hpp"
int main()
{
    sf::Image image;
    image.loadFromMemory(sfmlIconPngData, 3104); // exact size of array

    sf::RenderWindow window(sf::VideoMode(300, 16), "Include Image File As Header", sf::Style::Default);
    sf::Texture texture;
    texture.loadFromImage(image);
    sf::Sprite sprite(texture);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(sprite);
        window.display();
    }
    return EXIT_SUCCESS;
}

The header (icon_png.hpp) is attached.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

tyrbo321

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Failed to load image from memory, no data provided
« Reply #12 on: December 12, 2015, 08:36:04 pm »
i convert my png to .h now, but i cant find examples, and manuals how open it as image. I include .h now, ok, but as i try load it as image it does not work (not surprising). Maybe my question stupid, but how?
There has been talks of this on the forum before but I can't remember where.

Here's a simple example of how to load an image file from a header into an sf::Image:
#include <SFML/Graphics.hpp>
#include "icon_png.hpp"
int main()
{
    sf::Image image;
    image.loadFromMemory(sfmlIconPngData, 3104); // exact size of array

    sf::RenderWindow window(sf::VideoMode(300, 16), "Include Image File As Header", sf::Style::Default);
    sf::Texture texture;
    texture.loadFromImage(image);
    sf::Sprite sprite(texture);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(sprite);
        window.display();
    }
    return EXIT_SUCCESS;
}

The header (icon_png.hpp) is attached.
Thx man, without you I would not have solved the problem
« Last Edit: December 12, 2015, 09:11:50 pm by tyrbo321 »

Hapax

  • Hero Member
  • *****
  • Posts: 3349
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Failed to load image from memory, no data provided
« Reply #13 on: December 12, 2015, 09:44:48 pm »
You're very welcome.
Which part helped you, if you don't mind me asking?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

tyrbo321

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Failed to load image from memory, no data provided
« Reply #14 on: December 12, 2015, 10:17:01 pm »
You're very welcome.
Which part helped you, if you don't mind me asking?
This part.
image.loadFromMemory(sfmlIconPngData, 3104);
I don't wrote exact size of array, and tried to load as an image using loadFromFile.
« Last Edit: December 12, 2015, 10:19:16 pm by tyrbo321 »

 

anything