Search code examples
c++visual-studio-2012resourcesanimated-gifdirect2d

Visual Studio 2012 - How to add animated GIF as Resource for usage with Direct2d


I've written a class which is able to display an animated GIF file with Direct2D.

Right now I'm accessing the desired GIF to display via it's FilePath like:

WCHAR szFileName[MAX_PATH] = "C:\\Users\\xxx\\Desktop\\xxx.gif";

m_pIWICFactory->CreateDecoderFromFilename(  //My IWICImagingFactory
        szFileName,                         
        nullptr,           
        GENERIC_READ, 
        WICDecodeMetadataCacheOnLoad,
        &m_pDecoder);                       //My IWICBitmapDecoder

I need to change this part so that the desired GIF will be loaded from the Resources of my Project.

What I did/tried so far:

1)
- I right clicked on My Project, clicked add "Resource"
- In the popup I've selected "Import" and as Resource Type I defined "GIF"

Which resulted in a non-buildable project cause of Error RC2135 in C++ project due to UTF-8 encoding of RC file
Additionally it "destroyed" my GIF file. Opening the GIF in Notepad showed that it got converted from GIF89a to BM6(.bmp) during this process

2)
- I right clicked on My Project, clicked add "Exsisting Item" and selected my GIF
- Then i tried to add the give to the .rc file like IDR_MYGIF GIF ".\resources\xxx.gif"

Which results in "error RC2135: file not found: .\resources\xxx.gif "

So basically I need to know how I can add the GIF correctly to the Resources and how I'll be able to access it in the Code for the IWICBitmapDecoder

Thank you for any help


Solution

  • I finally got it working... here is what you have to do:

    1) Add your Resource to the project

    Right Click on the Project - Add - Existing Item - Select your file

    2) Edit your resource.h file

    Define a identifier for your resource, e.g.: #define IDR_GIF 107

    3) Edit your .rc file

    IDR_GIF RCDATA "C:\Users\xxx\Desktop\xxx.gif"

    4) Define a Stream Pointer

    I created a private member in my class, e.g.: LPSTREAM pStream;

    5) include Shlwapi.dll

    Add #include <Shlwapi.h>

    6) link Shlwapi.lib

    Project - "Your Project" Properties - Configuration Properties - Linker - Input - Additional Dependencies - Edit - Add Shlwapi.lib

    7) Use the resource

    HRSRC myResource = ::FindResource(NULL, MAKEINTRESOURCE(IDR_GIF), RT_RCDATA);
    HGLOBAL myResourceData = ::LoadResource(NULL, myResource);
    unsigned int myResourceSize = ::SizeofResource(NULL, myResource);
    
    pStream = SHCreateMemStream((LPBYTE)LockResource(myResourceData), myResourceSize);
    
    m_pIWICFactory->CreateDecoderFromStream(
            pStream,
            nullptr,
            WICDecodeMetadataCacheOnLoad,
            &m_pDecoder);