0

I'm trying to load a bitmap from resource instead of a file location. I'm using visual stuidos and I have imported the same bitmap into the rc. The LoadImage returns a NULL when I try with the resource version. Is my syntax wrong? or am I missing additional steps? Please point me in the right direction I'm trying to learn. Thanks in advance.

HBITMAP mhbitmap;

// Loading from a file works
mhbitmap = (HBITMAP)LoadImage(NULL, L"sblue.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
mpiccontrol.SetBitmap(mhbitmap);
if (mhbitmap == NULL) {
    MessageBox(L"null", L"from file", NULL);
}

The load from file version shows the image, but the resource version does not show the image.

mpiccontrol.SetBitmap(mhbitmap);

// My attempt at loading from resource
mhbitmap = (HBITMAP)LoadImage(NULL, MAKEINTRESOURCE("IDB_BITMAP1"), IMAGE_BITMAP, 0, 0, 0);
if (mhbitmap == NULL) {
    MessageBox(L"null", L"from rc", NULL);
}

mpiccontrol.SetBitmap(mhbitmap);
Terry
  • 19
  • 3
  • 1
    You must set the first parameter of LoadImage to the instance of the executable that contains the resource. – john Jan 15 '19 at 21:43
  • @john thank you that fixed the problem by using AfxGeIinstancehandle() – Terry Jan 15 '19 at 22:22

1 Answers1

1

MAKEINTRESOURCE macro accepts integer resource id so it should be MAKEINTRESOURCE(IDB_BITMAP1) where IDB_BITMAP1 is a resource identifier macro (probably from resource.h). You should also call GetLastError to figure out failure reason.

user7860670
  • 35,849
  • 4
  • 58
  • 84
  • I have made the correction of taking off the quotation marks, but it is still not showing the image. The "resource.h" does have the definition of IDB_BITMAP1. GetLastError() is telling me the last operation was completed successful. Am I missing anything else? Thanks – Terry Jan 15 '19 at 22:17
  • Seems like your problem is elsewhere. – Peter Ruderman Jan 16 '19 at 00:34