locked
How to paint to a bitmap using DirectX

    Question

  • I am trying to use DirectX to create and paint into a bitmap, and then later use that bitmap to paint to the screen.  I have the following test code which I have pieced together from a number of sources.  I seem to be able to create the bitmap without errors, and I try and fill it with red.  However, when I use the bitmap to paint to the screen I just get a black rectangle. 

    void PaintTestBitmap(GRectF& rect)
    {
        HRESULT                 hr;
        UINT                    creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;

        D3D_FEATURE_LEVEL featureLevels[] = 
        {
            D3D_FEATURE_LEVEL_11_1,
            D3D_FEATURE_LEVEL_11_0,
            D3D_FEATURE_LEVEL_10_1,
            D3D_FEATURE_LEVEL_10_0,
            D3D_FEATURE_LEVEL_9_3,
            D3D_FEATURE_LEVEL_9_2,
            D3D_FEATURE_LEVEL_9_1
        };

        ComPtr<ID3D11Device> device;
        ComPtr<ID3D11DeviceContext> context;
        ComPtr<ID2D1DeviceContext>    d2dContext;

        hr    = D3D11CreateDevice(    NULL,
                                    D3D_DRIVER_TYPE_HARDWARE,
                                    NULL,
                                    creationFlags,
                                    featureLevels,
                                    ARRAYSIZE(featureLevels),
                                    D3D11_SDK_VERSION,
                                    &device,
                                    NULL,
                                    &context);

        if (SUCCEEDED(hr))
        {
            ComPtr<IDXGIDevice> dxgiDevice;
            device.As(&dxgiDevice);

            // create D2D device context
            ComPtr<ID2D1Device> d2dDevice;
            hr    = D2D1CreateDevice(dxgiDevice.Get(), NULL, &d2dDevice);

            if (SUCCEEDED(hr))
            {
                hr    = d2dDevice->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2dContext);

                if (SUCCEEDED(hr))
                {
                    ID2D1Bitmap1        *    pBitmap;
                    D2D1_SIZE_U                size        = { rect.m_width, rect.m_height };
                    D2D1_BITMAP_PROPERTIES1    properties    = { { DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED }, 96, 96, D2D1_BITMAP_OPTIONS_TARGET, 0 };

                    hr    = d2dContext->CreateBitmap(size, (const void *) 0, 0, &properties, &pBitmap);

                    if (SUCCEEDED(hr))
                    {
                        d2dContext->SetTarget(pBitmap);
                        d2dContext->BeginDraw();    
                        d2dContext->Clear(D2D1::ColorF(D2D1::ColorF::Red)); // Clear the bitmap to Red
                        d2dContext->EndDraw();

                        // If I now use pBitmap to paint to the screen, I get a black rectange
                        // rather than the red one I was expecting
                    }
                }
            }
        }
    }

    Note that I am fairly new to both DirectX and Metro style apps.

    Thanks
    Saturday, July 14, 2012 7:11 PM

Answers

All replies