How to save a ID2D1Bitmap to a file using WIC?

心不动则不痛 提交于 2019-12-20 04:24:24

问题


I have a problem with saving a bitmap into a file. I'm using How to save ID2D1Bitmap to PNG file as a reference, but I have a different error than the one posted in that.

I get error 0x88990015 HRESULT, which means: The resource used was created by a render target in a different resource domain.

Here's my code:

void Wnd::SavePng(LPCWSTR Path,ID2D1Bitmap* pBit) {
    CComPtr<ID2D1RenderTarget> pRT;
    CComPtr<IWICBitmap> pB;
    CComPtr<IWICBitmapEncoder> pEncoder;
    CComPtr<IWICBitmapFrameEncode> pFrame;
    CComPtr<IWICStream> pStream;

    WICPixelFormatGUID format = GUID_WICPixelFormat32bppPBGRA;
    HRESULT Hr = m_pWICFactory->CreateBitmap(pBit->GetSize().width,pBit->GetSize().height,format,WICBitmapCacheOnLoad,&pB); 

    if (SUCCEEDED(Hr)) {
        D2D1_RENDER_TARGET_PROPERTIES RTProps = RenderTargetProperties();
        RTProps.pixelFormat = PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM,D2D1_ALPHA_MODE_PREMULTIPLIED);
        Hr = m_pDirect2dFactory->CreateWicBitmapRenderTarget(pB,&RTProps,&pRT);
    }

    if (SUCCEEDED(Hr)) {
        pRT->BeginDraw();
        pRT->Clear();
        pRT->DrawBitmap(pBit);
        Hr = pRT->EndDraw();
    }

    if (SUCCEEDED(Hr)) {
        Hr = m_pWICFactory->CreateStream(&pStream);
    }

    if (SUCCEEDED(Hr)) {
        Hr = pStream->InitializeFromFilename(Path,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(&pFrame,NULL);
    }

    if (SUCCEEDED(Hr)) {
        Hr = pFrame->Initialize(NULL);
    }

    if (SUCCEEDED(Hr)) {
        Hr = pFrame->SetSize(pBit->GetSize().width,pBit->GetSize().height);
    }

    if (SUCCEEDED(Hr)) {
        Hr = pFrame->SetPixelFormat(&format);
    }

    if (SUCCEEDED(Hr)) {
        Hr = pFrame->WriteSource(pB,NULL);
    }

    if (SUCCEEDED(Hr)) {
        Hr = pFrame->Commit();
    }

    if (SUCCEEDED(Hr)) {
        Hr = pEncoder->Commit();
    }
}

I understand that you can't use resources made by another factory with another, but there has to be a way to make this work.


回答1:


Your understanding of resource affinity is insufficient. Resources are device-specific rather than factory-specific. Yes, they tend to be factory-specific as well, but the key is the device specificity.

In your example, you are passing in a bitmap created by some other render target, which you then pass to the DrawBitmap method of a different render target. You may only draw a bitmap created by the same render target. This ensures that the bitmap and the render target (source and target) are in the same resource domain (address space).



来源:https://stackoverflow.com/questions/14261226/how-to-save-a-id2d1bitmap-to-a-file-using-wic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!