当将位图加载为 Windows 资源时,是否有办法保留 BITMAPFILEHEADER?
我一直致力于使用 SFML 1.4 (简单快速的多媒体库)和 C++ 来测试一些东西和 Visual C++ 2008 Express 版。 为了避免图形程序出现外部图像,我正在测试 sf ::Image::LoadFromMemory(const char * Data, std::size_t SizeInBytes) 函数,使用简单的资源脚本加载位图资源:
IDB_SPRITE BITMAP "sprite1.bmp"
在我的代码中加载图像以创建 sf::Image 使用此位图资源,我使用以下过程,由 Win32 API 函数组成(我排除了检查以确保 Win32 函数不返回 NULL 的代码,以缩短这一点):
HRSRC hResInfo = FindResource(NULL, MAKEINTRESOURCE(IDB_SPRITE), RT_BITMAP);
HGLOBAL hResData = LoadResource(NULL, hResInfo);
char * resourceData = reinterpret_cast<char *>(LockResource(hResData));
之后,我使用 sf::Image::LoadFromMemory 函数:
MyImage.LoadFromMemory(resourceData, SizeofResource(NULL, hResInfo));
但是,这不起作用(我收到未知文件类型错误)。 经过一些测试,我发现我传递给LoadFromMemory函数的位图数据不包括BITMAPFILEHEADER(前14个字节),我相信这是未知文件类型错误的原因。
我可以手动恢复 BITMAPFILEHEADER 并使 LoadFromMemory 函数正常工作。 但是,我想知道是否有某种方法可以保留资源数据中的 BITMAPFILEHEADER 以避免这样做?
I've been working on testing a few things out using SFML 1.4 (Simple and Fast Multimedia Library) with C++ and Visual C++ 2008 Express Edition. To avoid having external images with my graphical programs, I was testing out the sf::Image::LoadFromMemory(const char * Data, std::size_t SizeInBytes) function with Bitmap resources loaded using a simple resource script:
IDB_SPRITE BITMAP "sprite1.bmp"
In my code to load the image to create an sf::Image using this bitmap resource, I use the following procedure, consisting of Win32 API functions (I've excluded the code that checks to make sure the Win32 functions don't return NULL to shorten this a bit):
HRSRC hResInfo = FindResource(NULL, MAKEINTRESOURCE(IDB_SPRITE), RT_BITMAP);
HGLOBAL hResData = LoadResource(NULL, hResInfo);
char * resourceData = reinterpret_cast<char *>(LockResource(hResData));
After that, I use the sf::Image::LoadFromMemory function:
MyImage.LoadFromMemory(resourceData, SizeofResource(NULL, hResInfo));
However, this doesn't work (I get an unknown file type error). After some testing, I discovered that the bitmap data I pass to the LoadFromMemory function does not include the BITMAPFILEHEADER (the first 14 bytes), and I believe this is the cause of the unknown file type error.
I can restore the BITMAPFILEHEADER manually and get the LoadFromMemory function to work fine. However, I'm wondering if there is some way to preserve the BITMAPFILEHEADER in the resource data to avoid doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用自定义资源类型将保留整个文件。 更改资源脚本以使用 RCDATA 类型而不是 BITMAP 类型:
在 FindResource 函数调用中,使用 RT_RCDATA 而不是 RT_BITMAP:
有关详细信息:
RCDATA 资源
资源类型
Using a custom resource type will preserve the entire file. Change the resource script to utilize the RCDATA type as opposed to the BITMAP type:
In the FindResource function call, use RT_RCDATA instead of RT_BITMAP:
For more information:
RCDATA Resource
Resource Types
您可以将文件作为自定义资源而不是 RT_BITMAP 添加到资源中 - 这将完全按原样添加文件。 除非您还需要 ::LoadImage() 它。
You can add file to resources as a custom resource instead of RT_BITMAP -- this will add file exactly as it is. Unless you also need to ::LoadImage() it.