简体   繁体   中英

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?

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).

也许就是你要找的东西?

Here's the MSDN documentation about resource files.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM