How would I load a PNG image using Win32/GDI (no G

2019-01-06 17:22发布

Is it possible to load a PNG from a file into an HBITMAP using Win32 GDI functions? If not, what would be the lightest solution without using external libraries (like libpng)?

4条回答
ら.Afraid
2楼-- · 2019-01-06 17:54

You can do it with StretchDIBits API, but limited by OS/driver availability.

Consult MSDN documentation for details:

http://msdn.microsoft.com/en-us/library/dd145121(v=VS.85).aspx

http://msdn.microsoft.com/en-us/library/dd145107(VS.85).aspx


I sincerely apologize for misleading you guys interested in this issue. Let me correct my mistake. No StretchDIBits for PNG drawing. You'd better try WIC method or consider way to integrate GDI+ in your projects.

查看更多
Anthone
3楼-- · 2019-01-06 17:57

You can use the Windows Imaging Component to load PNG files (on Windows XP SP2 and later). See MSDN Magazine for an introduction on how to use the API and my blog post for a code sample that loads a PNG from an IStream and converts it to an HBITMAP.

查看更多
SAY GOODBYE
4楼-- · 2019-01-06 18:00

There is no need to use Windows Imaging Component, GDI+ or PNG library. You can use Icon functionality.

  1. Add new icon (ICO_PNG) to VC project resources with custom Width and Height (Resource Editor->Image->New Image Type). Copy Your png image here and use Fill Tool+transparent color to make icon transparent.

  2. Add Picture Control (IDC_PNG) to Your dialog (Type = Owner draw).

  3. Dialog procedure code:

switch (msg)
{
    ...

    case WM_DRAWITEM:
    {
        LPDRAWITEMSTRUCT pDIS = (LPDRAWITEMSTRUCT)lParam;
        if (pDIS->CtlID == IDC_PNG)
        {
            HICON hIcon = (HICON)LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(ICO_LOGO), IMAGE_ICON, 0, 0, LR_LOADTRANSPARENT); 
            DrawIconEx(pDIS->hDC, 0, 0, hIcon, 0, 0, 0, NULL, DI_NORMAL);
            DestroyIcon(hIcon);
            return TRUE;
        }
    }
}
查看更多
beautiful°
5楼-- · 2019-01-06 18:03

Don't think GDI supports png, have you looked at libpng

查看更多
登录 后发表回答