简体   繁体   English

如何将图像作为资源包含在C ++可执行文件中?

[英]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.

相关问题 C ++:如何在使用libcurl的Windows中运行可执行文件? - C++: How do you run an executable in windows that uses libcurl? 如果我将图像作为资源包含在Visual Studio项目中,这些图像是否被编译为可执行文件? - If I include images as resources in my Visual Studio project, do these images get compiled into the executable? 如何在C ++代码中包含模板? - How do you include a template in c++ code? 如何使用C ++在可执行文件中编辑资源? - How can I edit the resources in an executable using C++? 如何将 git 提交号包含到 c++ 可执行文件中? - how to include git commit-number into a c++ executable? 如何从MAC中的/ Library / Framework文件夹包含C ++文件 - How do you include files in C++ from the /Library/Framework folder in MAC 如何在XCode中正确包含Boost文件(C ++第三方库)? - How do you properly include Boost files (C++ third party library) in XCode? 什么时候需要在c ++中使用#include - When do you need to use #include in c++ 如何使用qmake为C ++项目设置可执行属性? - How do I set the executable attributes with qmake for a c++ project? 如何在调试模式下通过终端运行可执行 c++? - How do run executable c++ through terminal in debug mode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM