A recent Windows 10 update (probably and also mysteriously KB4577586) seems to have had the effect of causing calls to SelectObject with a bitmap handle as an argument to return NULL when performed in a printer-compatible device context.
The relevant lines can be seen in this bitmap printing example, which works fine on earlier Windows 10 versions, and seems to have worked fine for the past ten years. You can find a full minimal working example in this GitHub repository.
hBitmap = (HBITMAP)LoadImage(NULL,szFileName,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE|LR_VGACOLOR);
// [...]
HDC prn = GetPrinterDC(hwnd);
hdcMem = CreateCompatibleDC(prn);
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hBitmap);
// hbmOld is NULL at this point, indicating SelectObject failure
The problem stops occurring after uninstalling Windows update KB5000802, but this is obviously not a long-term fix.