4

I faced a problem when trying to save ID2D1Bitmap (that created from ID2D1HwndRenderTarget) to PNG File. The output image is just empty with white color. HRESULT returned from function call EndDraw() is -2003238894.

Thanks for any help.

Here is my code:

HRESULT CImageUtil::SaveBitmapToFile(PCWSTR uri,ID2D1Bitmap* pBitmap,ID2D1RenderTarget* pRenderTarget)
{

HRESULT hr = S_OK;

ID2D1Factory *pD2DFactory = NULL;
IWICBitmap *pWICBitmap = NULL;
ID2D1RenderTarget *pRT = NULL;
IWICBitmapEncoder *pEncoder = NULL;
IWICBitmapFrameEncode *pFrameEncode = NULL;
IWICStream *pStream = NULL;

if (SUCCEEDED(hr))
{
    hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory);
}

//
// Create IWICBitmap and RT
//

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 = pD2DFactory->CreateWicBitmapRenderTarget(
        pWICBitmap,
    rtProps,
        &pRT
        );
}

if (SUCCEEDED(hr))
{
    //
    // Render into the bitmap
    //
    pRT->BeginDraw();

pRT->Clear(D2D1::ColorF(D2D1::ColorF::White));


pRT->DrawBitmap(pBitmap);

    pRT->EndDraw();
}
if (SUCCEEDED(hr))
{

    //
    // Save image to file
    //
    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();
}

SafeRelease(&pD2DFactory);
SafeRelease(&pWICBitmap);
SafeRelease(&pRT);
SafeRelease(&pEncoder);
SafeRelease(&pFrameEncode);
SafeRelease(&pStream);

return hr;
}
Pravitha V
  • 3,308
  • 4
  • 33
  • 51
Jacky Nguyen
  • 51
  • 1
  • 2

1 Answers1

0

How would you even know if you had an error, since you just swallow errors and continue instead of logging where they came from? You get a non-zero hresult, so first figure out which function it comes from by adding a printf or fprintf after every single function call. And you have a glaring omission in the block:

if (SUCCEEDED(hr))
{
    //
    // Render into the bitmap
    //
    pRT->BeginDraw();

pRT->Clear(D2D1::ColorF(D2D1::ColorF::White));


pRT->DrawBitmap(pBitmap);

    pRT->EndDraw();
}
if (SUCCEEDED(hr))

You don't bother to assign hr anywhere in there, so you wouldn't even know if any of them are erroring out. Obviously, Clear() and the final png writing work fine, since you get a good file, so it's DrawBitmap or one of the bitmap creation calls that are failing.

SilverbackNet
  • 2,076
  • 17
  • 29
  • Yes, when I code like this: pRT->DrawBitmap(pBitmap); hr = pRT->EndDraw() and then hr = -2003238894. It means that the call DrawBitmap make error. And if I comment out line pRT->DrawBitmap(...), then hr = S_OK. – Jacky Nguyen Jul 12 '12 at 08:20
  • I guess that the error because I draw incompatible bitmap. I use WIC Render target to draw Hwnd Render Target's bitmap. But I don't know how to fix it. – Jacky Nguyen Jul 12 '12 at 08:39
  • 4
    I looked up your error code here: http://msdn.microsoft.com/en-us/library/windows/desktop/dd370979%28v=vs.85%29.aspx (0x88990012 = D2DERR_WRONG_FACTORY). It looks like the problem is pBitmap and pRT weren't created from the same ID2D1Factory object. – Esme Povirk Jul 12 '12 at 22:31
  • 1
    Thanks so much, your reply is very useful, I have a mistake by using different factory. The problem have been solved now. – Jacky Nguyen Jul 13 '12 at 15:07
  • @SilverbackNet: You could add this (ommission of hr checks) as a comment to the question and not as a solution. The real solution is what Vincent has given. – legends2k Dec 27 '12 at 14:55
  • @VincentPovirk: You could've added your comment as an answer – legends2k Dec 27 '12 at 14:56