Quantcast
Channel: Direct2D | codexpert blog
Viewing all articles
Browse latest Browse all 29

MFC Support for Direct2D – Part 4: Built-in Effects

$
0
0

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;
    pRenderTarget->GetRenderTarget()->QueryInterface(__uuidof(ID2D1DeviceContext), 
        reinterpret_cast<void**>(&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 = 
        pRenderTarget->GetRenderTarget();

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

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

    // finally, call ID2D1DeviceContext::DrawImage
    spDeviceContext->DrawImage(spEffect);
    //
    // 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 demo.zip (33)

Gaussian Blur Effect - Demo Project

Notes

Resources and related articles


Viewing all articles
Browse latest Browse all 29

Trending Articles