Search code examples
c++winapigdianimated-gif

How to load GDI::Image (GIF) from Resource in c++(winapi)?


I'm making a window c++ with winapi(non-MFC) and showing gif. For this Im using GDI++. I'm loading a gif to GDI::Image from the path, but I want to load it from resources. How can I do it?

    hMWDC = GetDC(hWnd);
    pGphcs = new Graphics(hMWDC);
    WCHAR path[MAX_PATH];
    GetModuleFileNameW(NULL, path, MAX_PATH);
    PathRemoveFileSpecW(path);
    PathAppendW(path, L"gifs\\test.gif");
    pImg = new Image(path);

    if (pImg) {
        nFrmCnt = pImg->GetFrameCount(&FrameDimensionTime);
        SetTimer(hWnd, DRAW_ANIM, 100, NULL);

    }
case WM_TIMER:
        if (wParam == DRAW_ANIM)
        {
            pImg->SelectActiveFrame(&FrameDimensionTime, nFrm);
            Rect DRC(0, 0, pImg->GetWidth(), pImg->GetHeight());
            pGphcs->Clear(Color(128, 128, 128));


            pGphcs->DrawImage(pImg, DRC);
            if (nFrm < (nFrmCnt - 1)) nFrm++; else nFrm = 0;
        }

        break;

Solution

  • There is an Image constructor that accepts an IStream*.

    You can create a stream by calling SHCreateMemStream on the raw buffer of a resource, which can be obtained by calling FindResource/LoadResource/LockResource and SizeOfResource.