如何使用WIC将ID2D1Bitmap保存到文件? [英] How to save a ID2D1Bitmap to a file using WIC?

查看:2347
本文介绍了如何使用WIC将ID2D1Bitmap保存到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将位图保存到文件时遇到问题。我正在使用如何将ID2D1Bitmap保存为PNG文件作为参考,但我有一个不同的错误比那个发布。

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.

我得到错误0x88990015 HRESULT,这意味着:使用的资源是由一个渲染目标在一个不同的资源域。

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

这是我的代码:

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();
    }
}

我知道您不能使用

推荐答案

您对资源亲和力的理解不足。资源是特定于设备的,而不是特定于工厂的。是的,他们也倾向于工厂特定,但关键是设备的特异性。

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.

在你的例子中,你传递的位图创建的一些其他的渲染目标,然后你传递给另一个渲染目标的DrawBitmap方法。您只能绘制由同一渲染目标创建的位图。这确保位图和渲染目标(源和目标)在同一资源域(地址空间)。

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).

这篇关于如何使用WIC将ID2D1Bitmap保存到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆