[Solved] WIN32 : PictureBox Does Not Display Picture


The following code sample works for me on Windows 10 (SHLoadDIBitmap API seems not valid for Windows 10. I use LoadImage API instead.). You can refer to.

C++ code in dialog box procedure:

case WM_INITDIALOG:
    hImage = LoadImage(NULL, L"full_path_to\\image3.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
    if (NULL == hImage)
        errCode = GetLastError();

    hwd_static_img = GetDlgItem(hDlg, IDC_STATIC6);
    SendMessage(hwd_static_img, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImage);
    return (INT_PTR)TRUE;

Resource script in project_name.rc file:

CONTROL         "",IDC_STATIC6,"Static", SS_BITMAP,37,133,136,109

Two notes:

  1. Make sure the image file is a valid bitmap file. For example, you can draw a picture and save as a bitmap using mspaint.exe. If you rename a file from .PNG to .BMP, LoadImage will return a NULL handle but GetLastError return 0 which indicate no error.
  2. Make sure set SS_BITMAP static control style for picture control (IDC_STATIC6).

BTW, no need to put image file in same directory with your project or EXE. Specify the valid full path of the image file will work.

0

solved WIN32 : PictureBox Does Not Display Picture