MFC Support for Direct2D – Built-in Effects

Direct2D has built-in support for image processing like changing brightness or contrast, blurring, creating drop shadows, and so on. Basically, we can use for this purpose the CreateEffect and DrawImage methods of ID2D1DeviceContext interface. So far, there is no MFC wrapper class for ID2D1DeviceContext but that’s not so big issue. Once having a valid ID2D1RenderTarget instance (wrapped in CRenderTarget class) we can easily get the ID2D1DeviceContext interface by calling QueryInterface. No sweat!

Getting ID2D1DeviceContext interface in an MFC application

    CComPtr<ID2D1DeviceContext> spDeviceContext;
    // ...

Or, a little bit easier, use CComQIPtr which calls QueryInterface for you.

    CComQIPtr<ID2D1DeviceContext> spDeviceContext = pRenderTarget->GetRenderTarget();
    // ...

Drawing an image using the Gaussian Blur built-in effect

Here is an example:

void CChildView::_DrawBlurredImage(CRenderTarget* pRenderTarget)
    // ...
    // get ID2D1DeviceContext interface
    CComQIPtr<ID2D1DeviceContext> spDeviceContext = 

    // create Gaussian Blur effect
    CComPtr<ID2D1Effect> spEffect;
    HRESULT hr = spDeviceContext->CreateEffect(CLSID_D2D1GaussianBlur, &spEffect);
    // ...

    // set the input image
    spEffect->SetInput(0, m_pBitmap->Get());

    // finally, call ID2D1DeviceContext::DrawImage
    // if it doesn't blew up until here, means that it works. :)
    // ...  

Just note that you should additionally include d2d1_1.h header and link your project to dxguid.lib. More details can be found in the attached demo project.

The demo project

Download: Gaussian Blur Effect (1524 downloads)

Gaussian Blur Effect - Demo Project


Resources and related articles

Leave a Comment