How do you include images as resources in a C++ ex

2019-02-23 18:14发布

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

3条回答
做个烂人
2楼-- · 2019-02-23 18:42

Here's the MSDN documentation about resource files.

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

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-02-23 18:42

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

查看更多
萌系小妹纸
4楼-- · 2019-02-23 18:54

Maybe this is what you're looking for?

查看更多
登录 后发表回答