c++winapivisual-studio-2019gdi+

C++ GDI+ how to get and load image from resource?


I'm trying to display a PNG image from my resource file using GDI+.

#include <Windows.h>
#include <objidl.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")
#pragma comment(lib, "dwmapi.lib")

...
auto CALLBACK BorderlessWindow::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) noexcept -> LRESULT {

    if (auto window_ptr = reinterpret_cast<BorderlessWindow*>(::GetWindowLongPtrW(hwnd, GWLP_USERDATA))) {
        auto& window = *window_ptr;

        switch (msg) {
        case WM_PAINT: {
            HDC          hdc;
            PAINTSTRUCT  ps;
            hdc = BeginPaint(hwnd, &ps);
           
            Graphics    graphics(hdc);
            Image image(L"C:\\my_image.png");
            graphics.DrawImage(&image, 50, 50);
           
            EndPaint(hwnd, &ps);
            break;
            }
        }
    }

    return ::DefWindowProcW(hwnd, msg, wparam, lparam);
}

...
int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {
   
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR           gdiplusToken;
    // Initialize GDI+.
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
   
    ...

    GdiplusShutdown(gdiplusToken);
}

The code above is working for the local file, I wanted to use the resource file that I added to my project. How can I do that?


Solution

  • TLDR: FindResource, LoadResource, and LockResource to get a pointer to the image bytes. Then make an IStream from it. The IStream can be used to initialize a Gdi+ Image or Bitmap object. (Gdiplus::Bitmap derives from Gdiplus::Image)

    Stick a new line into your .rc file:

    IDI_MY_IMAGE_FILE    PNG      "foo.png"
    

    And make sure IDI_MY_IMAGE_FILE is defined as an integer in your resource.h header file.

    #define IDI_MY_IMAGE_FILE               131
    

    Then to load the image at runtime:

    Gdiplus::Bitmap* pBmp = LoadImageFromResource(hInstance, MAKEINTRESOURCE(IDI_MY_IMAGE_FILE), L"PNG");
    

    Where LoadImageFromResource is a helper function that does all the heavy work of loading a PNG from your application resources.

    Gdiplus::Bitmap* LoadImageFromResource(HMODULE hMod, const wchar_t* resid, const wchar_t* restype)
    {
        IStream* pStream = nullptr;
        Gdiplus::Bitmap* pBmp = nullptr;
        HGLOBAL hGlobal = nullptr;
    
        HRSRC hrsrc = FindResourceW(hMod, resid, restype);     // get the handle to the resource
        if (hrsrc)
        {
            DWORD dwResourceSize = SizeofResource(hMod, hrsrc);
            if (dwResourceSize > 0)
            {
                HGLOBAL hGlobalResource = LoadResource(hMod, hrsrc); // load it
                if (hGlobalResource)
                {
                    void* imagebytes = LockResource(hGlobalResource); // get a pointer to the file bytes
    
                    // copy image bytes into a real hglobal memory handle
                    hGlobal = ::GlobalAlloc(GHND, dwResourceSize);
                    if (hGlobal)
                    {
                        void* pBuffer = ::GlobalLock(hGlobal);
                        if (pBuffer)
                        {
                            memcpy(pBuffer, imagebytes, dwResourceSize);
                            HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pStream);
                            if (SUCCEEDED(hr))
                            {
                                // pStream now owns the global handle and will invoke GlobalFree on release
                                hGlobal = nullptr;
                                pBmp = new Gdiplus::Bitmap(pStream);
                            }
                        }
                    }
                }
            }
        }
    
        if (pStream)
        {
            pStream->Release();
            pStream = nullptr;
        }
    
        if (hGlobal)
        {
            GlobalFree(hGlobal);
            hGlobal = nullptr;
        }
    
        return pBmp;
    }