Search code examples
c++visual-c++mfcdirect2dwic

Problems with ID2D1bitmap


I'm trying to save a ID2D1bitmap to a file according to this How to save ID2D1Bitmap to PNG file

Can this be done in windows 7? without any platform update?

I get an Unhandled exception. (Aceess violation reading) at :

if (SUCCEEDED(hr))
{
    hr = m_pWICFactory->CreateBitmap(
        sc_bitmapWidth,
        sc_bitmapHeight,
        GUID_WICPixelFormat32bppPBGRA,
        WICBitmapCacheOnLoad,
        &pWICBitmap
        );
}

I've declared m_pWICFactory & m_pDirect2dFactory as:

ID2D1Factory* m_pDirect2dFactory;
IWICImagingFactory *m_pWICFactory;

Can some one explain me the problem?


Solution

  • I'm pretty sure that you've a null m_pWICFactory (because the post you've linked to doesn't contain that code). Did you initialize it first before usage? It is usually done with a member function like so and is called before other operations that require the factory is performed.

    HRESULT CreateDeviceIndependentResources()
    {
        HRESULT hr;
    
        // Create a Direct2D factory.
        hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pDirect2dFactory);
    
        if (SUCCEEDED(hr))
        {
            // Create a WIC factory.
            CoCreateInstance(
                CLSID_WICImagingFactory,
                NULL,
                CLSCTX_INPROC_SERVER,
                IID_IWICImagingFactory,
                reinterpret_cast<void **>(&m_pWICFactory)
                );
        }
        return hr;
    }
    

    Refer Using the Windows Imaging Component in MSDN for more information.