如何为IWICBitmap创建ID2D1DeviceContext?(适用于C++中的Metro应用程序)

How to create ID2D1DeviceContext for IWICBitmap? (For Metro app in C++)

本文关键字:C++ 适用于 中的 Metro 应用程序 IWICBitmap 创建 ID2D1DeviceContext      更新时间:2023-10-16

我可以使用以下代码为IWICBitmap创建ID2D1RenderTarget。。。

    D2D1_FACTORY_OPTIONS options;
    ZeroMemory(&options, sizeof(D2D1_FACTORY_OPTIONS));
#if defined(_DEBUG)
     // If the project is in a debug build, enable Direct2D debugging via SDK Layers
    options.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;
#endif
    ThrowIfFailed(D2D1CreateFactory(
            D2D1_FACTORY_TYPE_SINGLE_THREADED,
            __uuidof(ID2D1Factory1),
            &options,
            &m_d2dFactory
            ));
    D2D1_RENDER_TARGET_PROPERTIES props;
    props = D2D1::RenderTargetProperties();
    m_d2dFactory->CreateWicBitmapRenderTarget(m_pTheBitmap.Get(), &props, &m_target);

但是,如果我想将ID2D1Effect应用于该位图,只有当我有ID2D1DeviceContext时,我才能做到这一点。如何获取IWICBitmap的ID2D1DeviceContext?

创建渲染目标后,需要QI到ID2D1DeviceContext。例如

pWicRenderTarget->QueryInterface(
                      __uuidof(ID2D1DeviceContext), 
                      reinterpret_cast<void**>(&pDC)
                      );

请记住,QI也在增加引用计数。