Thread: Loading Image from file path

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Loading Image from file path

    Being looking for an example in vain to load an image (bmp or jpg) from a folder on c:drive... LoadImage() function loads from resources, which i'm not very clear about either.. Few lines of code would help!

    thanx

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Something like:
    Code:
    HANDLE h;
    h = LoadImage(NULL,      // hInst
                             TEXT("blah.jpg"),    // lpszName
                             IMAGE_BITMAP,     // utype
                             0, 0,                       // cxDesired, cyDesired
                             LR_DEFAULTCOLOR);
    if (!h)
        // Error. 
    ...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Hi Mats, this is what i got from MSDN

    Code:
    HRESULT LoadImage(LPWSTR pszFileName,
        IUnknown *pDirectDraw,
        DDSURFACEDESC *pDDSurfaceDesc,
        GUID *pFormatID,
        REFIID riid,
        void **ppDXSurface
    );
    This doesn't match the example you gave, please correct me if am wrong...

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Below is how i've used it, but the handle "radarBmp" always returns NULL???

    Code:
    HBITMAP radarBmp = (HBITMAP)LoadImage( NULL,
    	TEXT("c:\\Quest Projects\\Visual Studio\\Resources\\Resources\\radar_bgrnd.bmp"),
    	IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    When you copy the path provided to the start\run menu is the file opened without problems?

    If the function fails, the return value is NULL. To get extended error information, call GetLastError.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Unhappy

    Quote Originally Posted by vart View Post
    to the start\run menu
    ?? not understanding

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What vart means is that you copy the name of your file to the "Start->Run..." menu on Windows. You would have to edit out the \\ into \ to make it work, but it's a good way to determine that the file is actually there.

    I find this a bit suspicious:
    Code:
    c:\\Quest Projects\\Visual Studio\\Resources\\Resources\\radar_bgrnd.bmp
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    What vart means is that you copy the name of your file to the "Start->Run..." menu on Windows. You would have to edit out the \\ into \ to make it work, but it's a good way to determine that the file is actually there.

    I find this a bit suspicious:
    Code:
    c:\\Quest Projects\\Visual Studio\\Resources\\Resources\\radar_bgrnd.bmp
    --
    Mats
    Opens with no probs on start->run... Well resources\resources are folders created by VS 2005.. Not sure why it always creates sub folders of same names

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by vart View Post
    When you copy the path provided to the start\run menu is the file opened without problems?
    Used GetLastError()

    Error: 1814

    The specified resource name cannot be found in the image file. ERROR_RESOURCE_NAME_NOT_FOUND
    The specified resource

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, I was trying something out yesterday, and realized that this only works on images that are part of the executable. I ended up using an MFC CImage - but if you don't use MFC for other stuff, then that's no help.

    I'm afraid, I don't actually know if Windows native API supports loading bitmaps from a file, and if so, what the API is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    More patience and less haste required(read the docs with a little more care):
    Quote Originally Posted by msdn, LoadImage
    LR_LOADFROMFILE
    Loads the stand-alone image from the file specified by lpszName (icon, cursor, or bitmap file).
    Change/OR the last parameter of LoadImage and you should be good to go.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Ken Fitlike View Post
    More patience and less haste required(read the docs with a little more care):


    Change/OR the last parameter of LoadImage and you should be good to go.
    Ah, of course.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Ken Fitlike View Post
    More patience and less haste required(read the docs with a little more care):


    Change/OR the last parameter of LoadImage and you should be good to go.
    moved the file to the c: drive, and changed the code with no avail...

    Code:
    radarBmp = (HBITMAP)LoadImage( NULL, TEXT("c:\\radar_bgrnd.bmp"),
    				IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    Error Number is zero this time but LoadImgae still returns null. In fact, when i debug and use the watch window to check value in radarBmp , it says
    Code:
    0x00000000 {unused=???}
    .. Why is it saying unused as if LoadImage() never assigned any value... Maybe the casting (HBITMAP) is not working

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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