c++bitmapdirect3d9

load BITMAP with D3DXLoadSurfaceFromMemory


Here is my unsuccessful try :

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

As you can see D3DXLoadSurfaceFromMemory returns D3DERR_INVALIDCALL.

Here is some debugging info :

renderTargetSurface properties :

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 properties :

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

What I'm doing wrong here?


Solution

  • bmp.bmBits is null. You need to pass LR_CREATEDIBSECTION flag into LoadImage, otherwise it will create a compatible bitmap and raw data won't be available.

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