user63899
user63899

Reputation:

How do you include images as resources in a C++ executable?

Is it possible include images (jpegs) as resources in a win32 c++ executable? If so how?

Upvotes: 5

Views: 13961

Answers (3)

Rob
Rob

Reputation: 78628

If it's Windows only then use a custom resource. If you want something cross-platform then do what I did for a recent project - create an app that will encode the JPEG as a char* buffer in a header file and then include these headers in your main project. You will also need to store the size of the buffer as it will be sure to contain NULs.

For example, I have an app that you can pass a load of files to be encoded and for each file you get a header file that looks something like this:

#ifndef RESOURCE_SOMEFILE_JPG_HPP
#define RESOURCE_SOMEFILE_JPG_HPP

namespace resource {

const char* SOMEFILE_JPG[] =
{
  ...raw jpeg data...
};

const int SOMEFILE_JPG_LEN = 1234;

} // resource

#endif // RESOURCE_SOMEFILE_JPG_HPP

The app has to escape special non-printable chars in \x format, but it's pretty simple. The app uses the boost::program_options library so a list of files to encode can be stored in a config file. Each file gets its own header like similar to the above.

However, be warned - this only works for small files as some compilers have a limit on the maximum size a static char buffer can be. I'm sure there are other ways to do this but this scheme works for me (a C++ web app that stores the HTML, CSS, JavaScript and image files in this way).

Upvotes: 5

John Smith
John Smith

Reputation: 4512

Here's the MSDN documentation about resource files.

http://msdn.microsoft.com/en-us/library/aa380599(VS.85).aspx

Upvotes: 1

Stefan
Stefan

Reputation: 43575

Maybe this is what you're looking for?

Upvotes: 0

Related Questions