从 .exe 文件中提取 .bmp

ulk*_*CST 0 c++ visual-c++-6 visual-c++

我在 VC++ 6 中有一个使用位图的项目。我想以编程方式提取一些位图并另存为 .bmp 文件。

请给我一些建议。

这些是我想做的几个步骤。

  1. 我在 VC++ 6 中有一个项目,其中包含我在 VC++ 位图编辑器中制作的一些位图。
  2. 我将单击 .exe 中的菜单。
  3. 该程序将从自身中提取所有 .bmp 文件(它已经有)并将它们保存在硬盘上作为 .bmp

提前致谢

乌尔卡

zet*_*t42 5

可执行文件中的位图资源只是去除了 BITMAPFILEHEADER 的位图文件。另请参阅:https : //blogs.msdn.microsoft.com/oldnewthing/20091211-00/?p=15693

所以你需要做的就是自己创建这个头文件,然后先把它写入文件。对于最常见的 24 bpp 位图来说,这相对容易,但如果您也想支持其他位深度,则需要更多的参与。

接下来写入通过 FindResource() / LoadResource() / LockResource() 调用获得的数据。

代码示例(使用 1 bpp、4 bpp、8 bpp 和 24 bpp 位图测试):

int main()
{
    // Obtain a handle to the current executable
    HMODULE hInst = ::GetModuleHandle( nullptr );

    // Locate and load a bitmap resource of the executable
    if( HRSRC hr = ::FindResource( hInst, MAKEINTRESOURCE( IDB_BITMAP1 ), RT_BITMAP ) )
        if( HGLOBAL hg = ::LoadResource( hInst, hr ) )
            if( auto pData = reinterpret_cast<const char*>( ::LockResource( hg ) ) )
            {
                DWORD resourceSize = ::SizeofResource( hInst, hr );

                // Check if we safely read the complete BITMAPINFOHEADER 
                // (to prevent a GPF in case the resource data is corrupt).
                if( resourceSize >= sizeof( BITMAPINFOHEADER ) )
                {
                    auto& bmih = reinterpret_cast<const BITMAPINFOHEADER&>( *pData );

                    // For simplicitly we can only save uncompressed bitmaps.
                    if( bmih.biCompression == BI_RGB )
                    {
                        // Calculate the size of the bitmap pixels in bytes.
                        // We use this to calculate BITMAPFILEHEADER::bfOffBits correctly.
                        // This is much easier than calculating the size of the color table.   
                        DWORD widthBytes = ( bmih.biBitCount * bmih.biWidth + 31 ) / 32 * 4;
                        DWORD heightAbs = abs( bmih.biHeight );  // height can be negative for a top-down bitmap!
                        DWORD pixelSizeBytes = widthBytes * heightAbs;

                        // Create the bitmap file header.
                        BITMAPFILEHEADER bfh = { 0 };
                        bfh.bfType = 0x4D42;                         // magic bytes: "BM"
                        bfh.bfSize = sizeof( bfh ) + resourceSize;   // total file size
                        bfh.bfOffBits = bfh.bfSize - pixelSizeBytes; // offset to bitmap pixels

                        // Write file header and bitmap resource data to file.
                        std::ofstream of( "mybitmap1.bmp", std::ios::binary );
                        of.write( reinterpret_cast<const char*>( &bfh ), sizeof( bfh ) );
                        of.write( pData, resourceSize );
                    }
                }
            }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我的原始答案遗漏了一个关键点(字面意思),即ios::binaryofstream 构造函数的标志。这就是为什么代码对 Arthur G 不起作用的原因。

那么为什么即使没有标志它实际上对我有用?有趣的是,这只是因为我的测试位图没有任何值为 10 的字节(永远不要相信程序员测试他自己的代码)!

默认情况下可能发生的一件事是行尾将根据平台默认值进行转换。也就是说,'\n'(ASCII 码 = 10)在 Windows 平台上将被转换为“\r\n”。当然,这会完全弄乱位图数据,因为任何真正的位图很可能在某处包含此值。

所以我们必须明确告诉 ofstream 我们的数据不应该被弄乱,这正是 ios::binary 标志所做的。

编辑2:

只是为了好玩,我扩展了我的示例,使其也适用于 1 位、4 位和 8 位每像素位图。这有点复杂,因为对于这些位深度,像素阵列不会在 BITMAPINFOHEADER 之后立即开始。在像素数组之前是颜色表,其大小必须添加到BITMAPFILEHEADER::bfOffBits.

根据 MSDN,事情更加复杂,因为即使位深度为 16 或更大,也可以有一个可选的颜色表(“以优化系统调色板的性能” - 无论如何)!请参阅https://msdn.microsoft.com/en-us/library/windows/desktop/dd183376 ( v= vs.85).aspx(在“biClrUsed”下)

因此,我没有考虑何时以及如何使用颜色表的所有脏细节,而是BITMAPFILEHEADER::bfOffBits通过从总资源大小(这是已知的)中减去像素数组的大小来计算。

计算像素数组的大小相当简单,但您必须注意以字节为单位的宽度四舍五入为下一个 4 的倍数(一个 DWORD)。否则,在保存宽度(以字节为单位)不是 4 的倍数的位图时会出错。

红利阅读

维基百科对位图文件格式有相当深入的描述,有一个很好的结构图:https : //en.wikipedia.org/wiki/BMP_file_format