在代码中嵌入图像,不使用资源部分或外部图像

2021-12-22 00:00:00 visual-c++ c++ mfc

我正在寻找一种在库中嵌入图像的方法(仅限 Windows).我不想走传统"的方式把它放在资源中(因为特殊情况使得乱七八糟的资源句柄不太方便.

I'm looking for a way to embed an image in a library (Windows-only). I don't want to go the 'traditional' way of putting it in the resources (because of special circumstances that make it not so convenient to mess around with the resource handle.

理想情况下,会有类似 xpm 文件的内容:图像的文本"表示,放入 ac 数组中,某些代码在内存中转换为位图,然后可以以某种方式将其加载到 HIMAGE 或 HICON.我要嵌入的图像是 32 位位图 (bmp).有任何想法吗?我正在使用 MFC,所以 MFC 库会很好,但当然我也可以使用不使用 MFC 的库.谢谢.

Ideally, there would be something like xpm files: a 'text' representation of an image that is put in a c array and that some code converts into a bitmap in memory, which can then somehow be loaded into an HIMAGE or an HICON. The images I want to embed are 32-bit bitmaps (bmp). Any ideas? I'm using MFC so an MFC library would be fine, but of course I can use a library that doesn't use MFC too. Thanks.

推荐答案

Google 的 bin2c 实用程序(类似于 http://stud3.tuwien.ac.at/~e0025274/bin2c/bin2c.c).它接受一个文件的二进制表示,并输出一个 C 源文件,其中包含一个初始化为该数据的字节数组.

Google for a bin2c utility (something like http://stud3.tuwien.ac.at/~e0025274/bin2c/bin2c.c). It takes a file's binary representation and spits out a C source file that includes an array of bytes initialized to that data.

只需将文件链接进去,您的图像就会保存在一块内存中.

Just link the file in and you have your image sitting in a chunk of memory.

在可能不存在诸如资源"甚至文件之类的东西的嵌入式系统中,使用这种工具非常普遍.

Using this kind of tool is really common on embedded systems where such things as 'resources' and even files might not exist.

相关文章