使用D3DXLoadSurfaceFromMemory加载BITMAP

时间:2017-09-25 14:46:40

标签: c++ bitmap direct3d9

这是我不成功的尝试:

IDirect3DSurface9 *renderTargetSurface;
BITMAP bmp;
HBITMAP hBMP = ( HBITMAP ) LoadImage ( 0, "D:\\DATA\\FLAG_B24.BMP", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
int bytes = GetObject ( hBMP, sizeof( BITMAP ), &bmp ); //bytes = 24
HRESULT res = m_pd3dDevice->CreateOffscreenPlainSurface ( bmp.bmWidth, bmp.bmHeight, D3DFMT_X8R8G8B8, 
                                                          D3DPOOL_SYSTEMMEM, &renderTargetSurface, NULL ); //res = S_OK
RECT r = { 0, 0, bmp.bmWidth, bmp.bmHeight };
res = D3DXLoadSurfaceFromMemory ( renderTargetSurface, NULL, NULL, bmp.bmBits, D3DFMT_X8R8G8B8,
                                  bmp.bmWidthBytes, NULL, &r, D3DX_FILTER_NONE, 0 ); //res = D3DERR_INVALIDCALL

您可以看到D3DXLoadSurfaceFromMemory返回D3DERR_INVALIDCALL

以下是一些调试信息:

renderTargetSurface属性:

Name = 0x00000000 <NULL>, Width = 22,
Height = 1, Usage = 205520896 ,Format = 2, Pool = D3DPOOL_DEFAULT (0),
MultiSampleType = D3DMULTISAMPLE_NONE (0), MultiSampleQuality = 124,
Priority = 124, LockCount = 2, DCCount = 22, CreationCallStack = 0x00000000 <NULL>

bmp属性:

bmType = 0, bmWidth = 124, bmHeight = 124,
bmWidthBytes = 496, bmPlanes = 1, bmBitsPixel = 32, bmBits = 0x00000000

我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

bmp.bmBits为空。您需要将LR_CREATEDIBSECTION标记传递给LoadImage,否则会创建兼容的位图,原始数据将无法使用。

const char * psz_file_path("D:\\DATA\\FLAG_B24.BMP");
const ::UINT flags(LR_LOADFROMFILE | LR_CREATEDIBSECTION);
HBITMAP hBMP(reinterpret_cast<::HBITMAP>(::LoadImage(0, psz_file_path, IMAGE_BITMAP, 0, 0, flags)));