locked
How to get bitmap data from ID3D11Texture2D? RRS feed

Answers

  • I just recently solved the same problem. Please see below. There might be other ways to do this but this is how I did it.

    You can get a IDXGISurface2 from ID3D11Texture2D and then use CreateSharedBitmap to get a bitmap data.

    ------Code -----

    ComPtr<ID3D11Texture2D> texture;

    CD3D11_TEXTURE2D_DESCbbDesc;

    bbDesc.Width =

    static_cast<UINT>(m_renderTargetSize.Width);

    bbDesc.Height =

    static_cast<UINT>(m_renderTargetSize.Height);

    bbDesc.Format =

    DXGI_FORMAT_B8G8R8A8_UNORM;

    bbDesc.Usage =

    D3D11_USAGE_DEFAULT;

                bbDesc.CPUAccessFlags = 0;

                bbDesc.BindFlags =

    D3D11_BIND_RENDER_TARGET| D3D11_BIND_SHADER_RESOURCE;

    bbDesc.ArraySize = 1;

    bbDesc.MipLevels = 1;

    bbDesc.MiscFlags = 0;

    bbDesc.SampleDesc.Count = 1;

    bbDesc.SampleDesc.Quality = 0;

    MEDIA::ThrowIfFailed(

    m_spDX11Device->CreateTexture2D(

    &bbDesc,

    nullptr,

    &texture

    )

    );

    ComPtr<IDXGISurface2> surface;

        MEDIA::ThrowIfFailed(

    texture.Get()->QueryInterface(

    __uuidof(IDXGISurface2), &surface)

    );

    DXGI_SURFACE_DESCdesc;

    D2D1_BITMAP_PROPERTIESbp;

    surface->GetDesc(&desc);

    bp.pixelFormat = PixelFormat(desc.Format,

    D2D1_ALPHA_MODE_IGNORE);

    bp.dpiX = bp.dpiY = 0.0f;

     

    ComPtr<ID2D1Bitmap> bitmap;

    m_d2dContext->CreateSharedBitmap(

    __uuidof(IDXGISurface2),

    surface.Get(),

    &bp,

    &bitmap

    );



    • Edited by mlee22ph Friday, October 5, 2012 4:25 AM
    • Proposed as answer by Jesse Jiang Monday, October 8, 2012 7:14 AM
    • Marked as answer by Jesse Jiang Wednesday, October 10, 2012 6:42 AM
    Friday, October 5, 2012 4:23 AM

All replies

  • I just recently solved the same problem. Please see below. There might be other ways to do this but this is how I did it.

    You can get a IDXGISurface2 from ID3D11Texture2D and then use CreateSharedBitmap to get a bitmap data.

    ------Code -----

    ComPtr<ID3D11Texture2D> texture;

    CD3D11_TEXTURE2D_DESCbbDesc;

    bbDesc.Width =

    static_cast<UINT>(m_renderTargetSize.Width);

    bbDesc.Height =

    static_cast<UINT>(m_renderTargetSize.Height);

    bbDesc.Format =

    DXGI_FORMAT_B8G8R8A8_UNORM;

    bbDesc.Usage =

    D3D11_USAGE_DEFAULT;

                bbDesc.CPUAccessFlags = 0;

                bbDesc.BindFlags =

    D3D11_BIND_RENDER_TARGET| D3D11_BIND_SHADER_RESOURCE;

    bbDesc.ArraySize = 1;

    bbDesc.MipLevels = 1;

    bbDesc.MiscFlags = 0;

    bbDesc.SampleDesc.Count = 1;

    bbDesc.SampleDesc.Quality = 0;

    MEDIA::ThrowIfFailed(

    m_spDX11Device->CreateTexture2D(

    &bbDesc,

    nullptr,

    &texture

    )

    );

    ComPtr<IDXGISurface2> surface;

        MEDIA::ThrowIfFailed(

    texture.Get()->QueryInterface(

    __uuidof(IDXGISurface2), &surface)

    );

    DXGI_SURFACE_DESCdesc;

    D2D1_BITMAP_PROPERTIESbp;

    surface->GetDesc(&desc);

    bp.pixelFormat = PixelFormat(desc.Format,

    D2D1_ALPHA_MODE_IGNORE);

    bp.dpiX = bp.dpiY = 0.0f;

     

    ComPtr<ID2D1Bitmap> bitmap;

    m_d2dContext->CreateSharedBitmap(

    __uuidof(IDXGISurface2),

    surface.Get(),

    &bp,

    &bitmap

    );



    • Edited by mlee22ph Friday, October 5, 2012 4:25 AM
    • Proposed as answer by Jesse Jiang Monday, October 8, 2012 7:14 AM
    • Marked as answer by Jesse Jiang Wednesday, October 10, 2012 6:42 AM
    Friday, October 5, 2012 4:23 AM
  • Thanks, I'll try it.
    Friday, October 5, 2012 7:17 AM