0

I am trying to find out why I cannot load an image from a resource.

  • Images work when lpszName is filename, when changed to resource they do not.

  • Images are created the same way (Can be interchanged, same problem)

  • Images are located inside the Resource and Resource.rc files.

The code:

LRESULT CALLBACK WndProc(HWND hWinMain, UINT message, WPARAM wParam, LPARAM lParam)
{
    DWORD
        lastError;
    static HDC
        hdcFromResource,
        hdcFromFilename;
    HBITMAP
        hFromResource,
        hFromResourcePrevious,
        hFromFilename,
        hFromFilenamePrevious;
    HDC 
        hdc;
    PAINTSTRUCT ps;
    switch (message)
    {
    case WM_CREATE:
        hdc = GetDC(hWinMain);
        hdcFromFilename = CreateCompatibleDC(hdc);
        hdcFromResource = CreateCompatibleDC(hdc);
        ReleaseDC(hWinMain, hdc);
        hFromFilename = (HBITMAP)LoadImageW(NULL, (L"filename.bmp"), IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE);
        if (!hFromFilename)
        {
            // ERROR HANDELING
        }
        hFromFilenamePrevious = (HBITMAP)SelectObject(hdcFromFilename, hFromFilename);
        DeleteObject(hFromFilename);
        DeleteObject(hFromFilenamePrevious);
        hFromResource = (HBITMAP)LoadImageW(NULL, MAKEINTRESOURCEW(IDB_RESOURCE), IMAGE_BITMAP, 100, 100, LR_CREATEDIBSECTION | LR_LOADFROMFILE);   
        lastError = GetLastError();
        lastError;
        hFromResourcePrevious = (HBITMAP)SelectObject(hdcFromResource, hFromResource);
        DeleteObject(hFromResource);
        DeleteObject(hFromResourcePrevious);
        return 0;
    case WM_PAINT:
        hdc = BeginPaint(hWinMain, &ps);
        BitBlt(hdc, 0, 0, 100, 100, hdcFromFilename, 0, 0, SRCCOPY);
        BitBlt(hdc, 100, 100, 100, 100, hdcFromResource, 0, 0, SRCCOPY);
        EndPaint(hWinMain, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWinMain, message, wParam, lParam);
}
  • The if(!hFromFilename) is never triggered whether the image loaded or not.

  • None of the breakpoints after the failed image load breaks (breakpoints through WM_CREATE, other breakpoints in other functions work as normal)

  • I am unsure how to easily read GetLastError without a breakpoint.

Resource.h #define IDB_RESOURCE 101

Resource.rc #define IDB_RESOURCE BITMAP DISCARDABLE "resource.bmp"

user4581301
  • 33,082
  • 7
  • 33
  • 54
Built on Sin
  • 351
  • 2
  • 6
  • 19
  • 2
    `LoadImageW(NULL, MAKEINTRESOURCEW(IDB_RESOURCE), IMAGE_BITMAP, 100, 100, LR_CREATEDIBSECTION | LR_LOADFROMFILE);` `LR_LOADFROMFILE` is for stand-alone image files, not embedded resources, so this needs to be removed. hInst should not be NULL for your own resources -- if these resources are in your exe, get hInst by calling `GetModuleHandle(NULL)` – Christopher Oicles Mar 04 '16 at 23:27
  • @HansPassant IDB_RESOURCE is "resource.bmp", there is no resource ID for "filename.bmp". – Built on Sin Mar 05 '16 at 00:30

1 Answers1

0

The problem as answered by Christopher Oicles was that LR_LOADFROMFILE is not needed.

May be useful for others:

  • To load an image from filename (Using LoadImage()) example:
LoadImage(
NULL,                           // NULL for load image via filename
L("directory\\filename.bmp"),   // String to filename
IMAGE_BITMAP                    // Flag for loading bitmap
0,                              // 0 Should use the images actual width
0,                              // 0 Should use the images actual height
LR_LOADFROMFILE                 // Flag for loading from file
);
  • To load an image from resource (Using LoadImage()) example:
LoadImage(
hInstance,                      // hInstance to the file containing the resource
MAKEINTRESOURCE(IDB_RESOURCE),  // Resource definition
IMAGE_BITMAP                    // Flag for loading bitmap
0,                              // 0 Should use the images actual width
0,                              // 0 Should use the images actual height
LR_CREATEDIBSECTION             // Do not use LR_LOADFROMFILE if it is a resource
);

Resourcefile.h

#define IDB_RESOURCE 101                         // Resource ID, and number

Resourcescript.rc

#include "Resourcefile.h"
IDB_RESOURCE BITMAP "directory\\filename.bmp"    // Resource ID, type, and filename

Thank you.

Built on Sin
  • 351
  • 2
  • 6
  • 19