Creating a HBITMAP from a LPD3DXBUFFER

Creating a HBITMAP from a LPD3DXBUFFER
First of all is this possible

Below is my current code with the annotated space where this transformation is required.















HBITMAP MyClass::ScreenGrab()
{


   //RenderTargetSurface.
   IDirect3DSurface9* pRenderTargetSurface = NULL;
   //DestinationTargetSurface
   IDirect3DSurface9* pDestinationTargetSurface = NULL;
   //DisplayMode
   D3DDISPLAYMODE d3dDipMode;
   //HBITMAP that will be the return of the method
   HBITMAP hbm;
   if (m_pd3dDevice == NULL)
      return NULL;
   //Get the client rectangle
   RECT rc;
   GetClientRect(myhWnd, &rc);
   ClientToScreen(myhWnd, LPPOINT(&rc.left));
   ClientToScreen(myhWnd, LPPOINT(&rc.right));

   //GetRenderTargetSurface.
   if(FAILED(m_pd3dDevice->GetRenderTarget(0, &pRenderTargetSurface)))
    return NULL;
   //Display Mode (d3dDipMode)
   if(FAILED(m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3dDipMode)))
       return NULL;
  
   //GetDestinationTargetSurface
   if(FAILED(m_pd3dDevice->CreateOffscreenPlainSurface((rc.right - rc.left),
                                                      (rc.bottom - rc.top),
                                                      d3dDipMode.Format,
                                                      D3DPOOL_SYSTEMMEM,
                                                      &pDestinationTargetSurface,
                                                      NULL)))
               return NULL;
   //copy RenderTargetSurface -> DestTarget
   if(FAILED(m_pd3dDevice->GetRenderTargetData(pRenderTargetSurface, pDestinationTargetSurface)))
    return NULL;
   LPD3DXBUFFER bufferedImage = NULL;
   //Save the DestinationTargetSurface into memory (as a bitmap)
   if(FAILED(D3DXSaveSurfaceToFileInMemory(&bufferedImage,
                                         D3DXIFF_BMP,
                                          pDestinationTargetSurface,
                                          NULL,
                                          NULL)))
    return NULL;

   //TRANSFORMATION REQUIRED HERE!!!!
   //I WANT TO TRANSFORM bufferedImage INTO A HBITMAP SO THAT THE METHOD RETURNS
   //THAT HBITMAP TO FULFIL MY CONTRACT.



  


//Release Resources
   pRenderTargetSurface->Release();
   pDestinationTargetSurface->Release();
   //Return HBITMAT
   return hbm;
}


.

Thanks for your help in advance,


Alex.


Answer this question

Creating a HBITMAP from a LPD3DXBUFFER

  • bignack

     CHTHONIC wrote:
    Creating a HBITMAP from a LPD3DXBUFFER
    First of all is this possible
    Yes, just use CreateDIBitmap().  You'll need read the Windows Platform SDK documentition on bitmaps to learn how to calculate the various pointer values you need. After you've created the bitmap, you can release the ID3DXBuffer pointer.

  • PrasannaGPKBE

    I have just looked at CreateDIBitmap() in the SDK documentation and googled for some decent examples. This looks horrendous! If you could point me in the right direction with a little example I would be forever in your debt.

    Thanks,

    Alex.


  • BBA

    Made a few cockups with my original question, it is now editted and correct. Sorry about that!

    Alex.

  • kangalert

     Jack Hoxley wrote:
    The OP asked the same question over at GDNet and it seems to have gather more replies. Anyone else with a similar problem may wish to check out the discussion here.
    Unfortnately, the the person who said on that forum the BMP file wasn't the same format as CreateDIBitmap() expects was wrong.  The BMP file format is a packed windows DIB with a header in front of it.  You just need to do some simple pointer arithmetic to obtain the the pointer values you need to pass CreateDIBitmap().  Read the Platform SDK documentation I linked to earlier to learn more.

  • steve brsk

    I'm just adding this for reference purposes

    The OP asked the same question over at GDNet and it seems to have gather more replies. Anyone else with a similar problem may wish to check out the discussion here.

    Cheers,
    Jack


  • Creating a HBITMAP from a LPD3DXBUFFER