Thread: Loading Image from file path

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    I don't know what is wrong, but I can eliminate the suspicion of "casting HANDLE to HBITMAP" from the candidates - that should not change the value of the handle, just it's type.

    --
    Mats
    Even changed the code to this

    Code:
    HANDLE hndl = LoadImage( NULL,TEXT("c:\\radar_bgrnd.bmp"),
    	IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    
    	DWORD err = GetLastError();
    
    	if (!hndl){
    	//MessageBox(hWnd, "Could not open file.", "File Error",  MB_ICONEXCLAMATION);
    	PostQuitMessage(WM_QUIT);
           }
    GetLastError() is convinced the image was loaded successfully cause it returns 0, but hndl recieves a NULL ... confusing!!

    BTW, why does MessageBox sends about 6 "File Error" message before the window closes?

  2. #17
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    If the operation is completing successfully then you are loading a valid bitmap. I'd be tempted to look to variable declaration/naming - with some fonts hndl and hnd1 are indistinguishable to the eye. Do you have any other similar and compatible type variable declarations in the same scope as this bitmap handle?

    Perhaps it would be prudent at this point to post a complete, minimal compilable example that replicates the problem so that we can better help you?

    edit:

    Try this and see if this helps. It should open a selected bitmap from file and briefly display it in a static control.
    Code:
    #include <windows.h>
    #include <tchar.h>
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,PSTR pStr,int nCmd)
    {
    OPENFILENAME ofn;      
    TCHAR szFile[MAX_PATH]=_T(""); 
    
    ZeroMemory(&ofn,sizeof(ofn));
    
    ofn.lStructSize = sizeof(ofn);
    ofn.lpstrFile   = szFile;
    ofn.lpstrTitle  = _T("Open bitmap...");
    ofn.lpstrFile   = szFile;
    ofn.nMaxFile    = sizeof(szFile);
    ofn.lpstrFilter = _T("Bitmap\0*.bmp\0");
    ofn.Flags       = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
    
    if(GetOpenFileName(&ofn) == TRUE) 
      {
      HBITMAP hBmp=LoadImage(0,szFile,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
      if (hBmp)
        {
        HWND hStatic=CreateWindowEx(WS_EX_CLIENTEDGE,_T("static"),0,
                                    WS_CHILD|WS_VISIBLE|SS_BITMAP,
                                    0,0,400,400,GetDesktopWindow(),0,hInst,0);
        SendMessage(hStatic,STM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hBmp);
        Sleep(1000);
        }
      else
        {
        TCHAR errbuff[64];
        DWORD dw=GetLastError();
        wsprintf(errbuff,_T("LoadImage failed with error code: %d"),dw);
        MessageBox(0,errbuff,_T("LoadImage Failed"),MB_OK|MB_ICONERROR);
        }
      }
    
    return 0;
    }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Image Loading Troubles
    By frenchfry164 in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2003, 01:07 PM