简体   繁体   English

C++ GDI+如何从资源中获取和加载图像?

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

I'm trying to display a PNG image from my resource file using GDI+.我正在尝试使用 GDI+ 从我的资源文件中显示 PNG 图像。

#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?我怎样才能做到这一点?

TLDR: FindResource, LoadResource, and LockResource to get a pointer to the image bytes. TLDR:FindResource、LoadResource 和 LockResource 用于获取指向图像字节的指针。 Then make an IStream from it.然后从中创建一个 IStream。 The IStream can be used to initialize a Gdi+ Image or Bitmap object. (Gdiplus::Bitmap derives from Gdiplus::Image) IStream 可用于初始化 Gdi+ 图像或 Bitmap object。(Gdiplus::Bitmap 派生自 Gdiplus::Image)

Stick a new line into your.rc file:在你的 .rc 文件中插入一个新行:

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.并确保在您的 resource.h header 文件中将 IDI_MY_IMAGE_FILE 定义为 integer。

#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.其中LoadImageFromResource是一个助手 function,它完成从应用程序资源加载 PNG 的所有繁重工作。

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(hInst, 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM