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"