13

I tried the following code on OnInitDialog() but nothing was shown.

m_staticLogo.SetBitmap(::LoadBitmap(NULL, MAKEINTRESOURCE(IDB_LOGO)));

where m_staticLogo is the static picture control and IDB_LOGO is the resource ID of the png file.

1

4 Answers 4

17

As you’ve discovered, ::LoadBitmap (and the newer ::LoadImage) only deal with .bmps. By far the easiest solution is to convert your image to a .bmp.

If the image has transparency, it can be converted into a 32-bit ARGB bitmap (here is a tool called AlphaConv that can convert it). Then load the image using the CImage class LoadFromResource method. Pass the CImage to m_staticLogo.SetBitmap().

But if you really need it to be a .png, it can be done.

Method 1 (the easier way): Load the .png from a file using CImage::Load. Pass the CImage to m_staticLogo.SetBitmap().

Method 2 (the harder way): Load the .png from a resource by loading the resource into a COM IStream and using CImage::Load. (NOTE: CImage::LoadFromResource looks tempting but will not work with a .png graphic). To get the resource into a COM IStream, see this Codeproject article. Note the article works with Gdiplus::Bitmap but the key part is how to create the IStream, which you should be able to adapt for CImage. Finally, pass the CImage to m_staticLogo.SetBitmap().

Edit: Updated to use CImage, which is easier than Gdiplus::Bitmap.

4
  • I converted the png file to bmp but the problem is that the background of the image is not transparent anymore after the conversion. Is there any way to convert and yet still preserve the transparent background? Oct 22, 2010 at 3:19
  • Method 1 works but I would prefer to have the graphic in the resource file. Method 2 is too difficult for me. Oct 22, 2010 at 3:23
  • You can preserve transparency by converting the .png to a 32-bit “ARGB” .bmp. Standard tools like Paint.NET will not do this. I’ve updated the article with a tool that works. It may look strange when viewed in Visual Studio but renders fine. This works well against one-color backgrounds. It won’t work if you have a complex background like a photo.
    – Nate
    Oct 22, 2010 at 9:10
  • Method 1: I am using CPngImage and loading from resources. Works great. But, does the PNG need to be a member variable of my dialog, or can I declare it in OnInitDialog when I am setting up the picture control? Mar 7 at 18:35
5

For those, who need quick solution, here is a way to load png file from resources using GDI+ (original answer for standard GDI from here - http://www.codeproject.com/Questions/377803/How-to-load-PNG-images-in-mfc):

bool GdiPlusUtils::LoadBitmapFromPNG(UINT uResourceID, 
    Bitmap** ppBitmapOut, HINSTANCE hInstance /*= NULL*/)
{
    bool bRet = false;

    if (!hInstance)
        hInstance = AfxGetInstanceHandle();

    HRSRC hResourceHandle = ::FindResource(
        hInstance, MAKEINTRESOURCE(uResourceID), L"PNG");
    if (0 == hResourceHandle)
    {
        return bRet;
    }

    DWORD nImageSize = ::SizeofResource(hInstance, hResourceHandle);
    if (0 == nImageSize)
    {
        return bRet;
    }

    HGLOBAL hResourceInstance = ::LoadResource(hInstance, hResourceHandle);
    if (0 == hResourceInstance)
    {
        return bRet;
    }

    const void* pResourceData = ::LockResource(hResourceInstance);
    if (0 == pResourceData)
    {
        FreeResource(hResourceInstance);
        return bRet;
    }

    HGLOBAL hBuffer = ::GlobalAlloc(GMEM_MOVEABLE, nImageSize);
    if (0 == hBuffer)
    {
        FreeResource(hResourceInstance);
        return bRet;
    }

    void* pBuffer = ::GlobalLock(hBuffer);
    if (0 != pBuffer)
    {
        CopyMemory(pBuffer, pResourceData, nImageSize);
        IStream* pStream = 0;
        if (S_OK == ::CreateStreamOnHGlobal(hBuffer, FALSE, &pStream))
        {
            *ppBitmapOut = new Bitmap(pStream);
            pStream->Release();
            bRet = true;
        }
        ::GlobalUnlock(hBuffer);
    }
    ::GlobalFree(hBuffer);

    UnlockResource(hResourceInstance);
    FreeResource(hResourceInstance);

    return bRet;
}

You can add png file as resource using Add Resource command and in the panel choose Import.

2
  • Can you give an example of the proper way of calling LoadBitmapFromPNG() ? May 16, 2020 at 17:54
  • It's easy, just use: Bitmap* pBitmap = 0; LoadBitmapFromPNG(IDB_PNG_YOUR_RESOURCE_ID, &pBitmap); where Bitmap* is GDI+ bitmap class and IDB_PNG_YOUR_RESOURCE_ID is ID of your resource pointing to bitmap. May 18, 2020 at 7:04
0

Bitmap and icon it supports. Not sure about png. Alternately, May be you can try the following.

  1. open png in MS Paint or some other viewer.
  2. Then copy the image portion from that.
  3. Create a resource in MFC resource.
  4. Paste the copied image to newly created resource.
  5. Use new resource id in LoadBitmap.
-1

If you are converting .png image file to .bmp format, you can end up with image clarity. So, catch WM_PAINT message in dialog box class and use

Graphics::DrawImage method

To obtain this method link your project with gdiplus.lib library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.