0

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

Community
  • 1
  • 1
Jan Raufelder
  • 287
  • 7
  • 23
  • Note: In "2)" I tried various paths resulting in the same error – Jan Raufelder Jul 27 '16 at 15:47
  • CreateDecoder *From Filename* does exactly what it promises. Use CreateDecoderFromStream() instead. CreateStreamOnHGlobal() is a simple way to obtain the IStream you need. – Hans Passant Jul 27 '16 at 15:49
  • @HansPassant If you'd provide a short desc of how to add the GIF to the Resources and then how to make use of it i could mark it as answer... – Jan Raufelder Jul 28 '16 at 07:27

1 Answers1

0

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);
Jan Raufelder
  • 287
  • 7
  • 23
  • `pStream` should either be an `IStream*`, or some smart pointer type. Since we cannot see the code after `CreateDecoderFromStream`, there's no way to know for sure, whether the stream object is properly released (by calling `Release`). – IInspectable Aug 02 '16 at 21:30