[英]How do you include images as resources in a C++ executable?
Is it possible include images (jpegs) as resources in a win32 c++ executable? 是否可以将图像(jpegs)作为win32 c ++可执行文件中的资源包含在内? If so how?
如果是这样的话?
If it's Windows only then use a custom resource. 如果它只是Windows,那么使用自定义资源。 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. 如果你想要跨平台的东西然后做我为最近的项目做的事情 - 创建一个应用程序,将JPEG编码为头文件中的
char*
缓冲区,然后在主项目中包含这些标题。 You will also need to store the size of the buffer as it will be sure to contain NULs. 您还需要存储缓冲区的大小,因为它肯定会包含NUL。
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. 该应用程序必须以
\\x
格式转义特殊的非可打印字符,但它非常简单。 The app uses the boost::program_options
library so a list of files to encode can be stored in a config file. 该应用程序使用
boost::program_options
库,因此要编码的文件列表可以存储在配置文件中。 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).
我确信还有其他方法可以做到这一点,但这个方案适合我(一个以这种方式存储HTML,CSS,JavaScript和图像文件的C ++ Web应用程序)。
也许这就是你要找的东西?
Here's the MSDN documentation about resource files. 这是关于资源文件的MSDN文档。
http://msdn.microsoft.com/en-us/library/aa380599(VS.85).aspx http://msdn.microsoft.com/en-us/library/aa380599(VS.85).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.