user2557850
user2557850

Reputation: 109

Wrong render target ID2D1Bitmap

I'm trying to save a ID2D1Bitmap based on this How to save ID2D1Bitmap to PNG file but unlike him at hr=pRT2->EndDraw(); my hr =-2003238891. which means D2DERR_WRONG_RESOURCE_DOMAIN. how do i use the source render target (pRenderTarget) to fix this?

bitmapToFile.cpp

HRESULT bitmapToFile::SaveBitmapToFile(PCWSTR uri,ID2D1Bitmap* pBitmap,ID2D1RenderTarget* pRenderTarget)
{
    HRESULT hr = S_OK;

    IWICBitmap *pWICBitmap = NULL;
    ID2D1RenderTarget *pRT2 = NULL;
    IWICBitmapEncoder *pEncoder = NULL;
    IWICBitmapFrameEncode *pFrameEncode = NULL;
    IWICStream *pStream = NULL;

    if (SUCCEEDED(hr))
    {
        // Create a WIC factory.
        CoCreateInstance(
            CLSID_WICImagingFactory,
            NULL,
            CLSCTX_INPROC_SERVER,
            IID_IWICImagingFactory,
            reinterpret_cast<void **>(&m_pWICFactory)
            );
    }

    UINT sc_bitmapWidth = pBitmap->GetSize().width;
    UINT sc_bitmapHeight = pBitmap->GetSize().height;

    if (SUCCEEDED(hr))
    {
        hr = m_pWICFactory->CreateBitmap(
            sc_bitmapWidth,
            sc_bitmapHeight,
            GUID_WICPixelFormat32bppPBGRA,
            WICBitmapCacheOnLoad,
            &pWICBitmap
            );
    }

    if (SUCCEEDED(hr))
    {

        D2D1_RENDER_TARGET_PROPERTIES rtProps = D2D1::RenderTargetProperties();
        rtProps.pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED);
        rtProps.type = D2D1_RENDER_TARGET_TYPE_DEFAULT;
        rtProps.usage = D2D1_RENDER_TARGET_USAGE_NONE;

        hr = m_pD2DFactory2->CreateWicBitmapRenderTarget(
            pWICBitmap,
            rtProps,
            &pRT2
            );
    }

    if (SUCCEEDED(hr))
    {
        pRT2->BeginDraw();

        pRT2->Clear();

        pRT2->DrawBitmap(pBitmap);

        hr=pRT2->EndDraw();
    }
    if (SUCCEEDED(hr))
    {
        hr = m_pWICFactory->CreateStream(&pStream);
    }

    WICPixelFormatGUID format = GUID_WICPixelFormat32bppPBGRA;
    if (SUCCEEDED(hr))
    {

        hr = pStream->InitializeFromFilename(uri, GENERIC_WRITE);
    }
    if (SUCCEEDED(hr))
    {
        hr = m_pWICFactory->CreateEncoder(GUID_ContainerFormatPng, NULL, &pEncoder);
    }
    if (SUCCEEDED(hr))
    {
        hr = pEncoder->Initialize(pStream, WICBitmapEncoderNoCache);
    }
    if (SUCCEEDED(hr))
    {
        hr = pEncoder->CreateNewFrame(&pFrameEncode, NULL);
    }
    if (SUCCEEDED(hr))
    {
        hr = pFrameEncode->Initialize(NULL);
    }

    if (SUCCEEDED(hr))
    {
        hr = pFrameEncode->SetSize(sc_bitmapWidth, sc_bitmapHeight);
    }
    if (SUCCEEDED(hr))
    {
        hr = pFrameEncode->SetPixelFormat(&format);
    }
    if (SUCCEEDED(hr))
    {
        hr = pFrameEncode->WriteSource(pWICBitmap, NULL);
    }
    if (SUCCEEDED(hr))
    {
        hr = pFrameEncode->Commit();
    }
    if (SUCCEEDED(hr))
    {
        hr = pEncoder->Commit();
    }

    return hr;
}

Upvotes: 2

Views: 1783

Answers (1)

VitaliyK
VitaliyK

Reputation: 299

The main issue is that both render targets(source ID2D1RenderTarget and destination WIC rendertarget) created with default render properties. According to https://msdn.microsoft.com/en-us/library/windows/desktop/dd756757(v=vs.85).aspx#sharing_render_target_resources resources cannot be shared between targets with default properties since it is not known where the bitmap will be rendered. So in my case I created both targets with these properties:

auto rtProps = D2D1::RenderTargetProperties();    
rtProps.type = D2D1_RENDER_TARGET_TYPE_SOFTWARE;    
rtProps.usage = D2D1_RENDER_TARGET_USAGE_GDI_COMPATIBLE;

After that WIC render successfully draw the source bitmap.

Upvotes: 2

Related Questions