使用 D3DXLoadSurfaceFromMemory 加载 BITMAP

load BITMAP with D3DXLoadSurfaceFromMemory

这是我不成功的尝试:

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 returns 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

我哪里做错了?

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)));