Thread: LoadImage()

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    162

    LoadImage()

    Hi

    I am having difficulties with the LoadImage() function

    This is how I use it...
    Code:
    HBITMAP hCross = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(101), IMAGE_BITMAP, 15, 15, LR_SHARED);
    DrawState(hDC, NULL, NULL, (LPARAM)hCross, NULL, m_rcRect[i].left, m_rcRect[i].top, 15, 15, DST_BITMAP);
    (The Drawstate works ok...)

    The LoadImage() seems it does not take in the resource files... I tried to look it up, but with no success, the documentation does not say much...

    Can you please help me with this or advise me with any other function that is able to draw bitmaps from resource...

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Shouldn't '101' be IDI_MYIMAGE (or whatever the assigned name may be)?

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by Oldman47 View Post
    Shouldn't '101' be IDI_MYIMAGE (or whatever the assigned name may be)?
    Yes it could, but its not necessary...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Gordon View Post
    Yes it could, but its not necessary...
    No, it's not necessary, but it makes it easier to read the code half a year later.

    --
    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.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    What does LoadImage() return? ie a valid HBITMAP?

    If LoadImage() fails, what does GetLastError() return?

    EDIT:
    Why are you using 'SHARED'. It may not open because you are changing the size.
    Is the image a bitmap? (or is it a cursor / icon which have different flags for the uType)
    Last edited by novacain; 12-20-2007 at 07:58 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by novacain View Post
    What does LoadImage() return? ie a valid HBITMAP?

    If LoadImage() fails, what does GetLastError() return?

    EDIT:
    Why are you using 'SHARED'. It may not open because you are changing the size.
    Is the image a bitmap? (or is it a cursor / icon which have different flags for the uType)
    Ok sorry... Let me change the code

    If I use this code
    Code:
    HBITMAP hCross = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(IDB_CROSS), IMAGE_BITMAP, 15, 15, LR_DEFAULTSIZE);
    Does not work either

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have you checked that the return result from the function is not NULL? I'm not sure why it would be, but if it is, I don't expect the next function to "do much". If that is the case, then you should, as suggested, use "GetLastError" to figure out what is "wrong".

    --
    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.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The only reason that line of code would not work is either the resource does not exist. This is unlikely since you would get an undefined symbol error for your resource identifier. If you are trying to load system provided bitmaps (such as cross, mouse arrow, etc,) I believe there is another approach. LoadImage I believe is for custom resource and non-resource bitmaps. It works in my tile editor and my code is not much diff than what you have so I don't know why it's not working given the code you have shown.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    I have done a few investigations about this issue...

    I tried to create a brand new project

    Code:
    void Draw(HWND hWnd)
    {
    	HDC hDC = GetDC(hWnd);
    	HDC hOffDC = CreateCompatibleDC(hDC);
    	HBITMAP hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));        
    //HBITMAP hBitmap = (HBITMAP)LoadImage(hInstance, TEXT("bitmap1.bmp"), IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE);	
            SelectObject(hOffDC, hBitmap);
    
    	BitBlt(hDC, 100, 100, 800, 600, hOffDC, 0, 0, SRCCOPY);
    }
    In each case of drawing I use only one line either blue or red... The blue one works well... The red does not work...

    But If I try the same code in different project both work well... I counldt find out the difference between those two projects...

    These may cause the issue... What do you suppose?
    1) Missing header file
    2) Different settings of the project
    3) Different setting of the resource file

    I have no idea how come in one project same code work and in different fail.

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Could you please follow the advice given to you twice in this thread and post the results of that?

    That is:

    Is the returned value from LoadImage using the bitmap resource non-NULL?

    If the returned value from LoadImage is NULL what does GetLastError tell you about why it is NULL.

    Until you do this - or, if you have already tried this - until you post the results of this, there's just going to be a lot of pointless guessing.

    If the return value from LoadImage is non-NULL then create a minimal, compilable example that replicates the problem and post or attach it. This, too, will better help us to help you.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by Ken Fitlike View Post
    Could you please follow the advice given to you twice in this thread and post the results of that?

    That is:

    Is the returned value from LoadImage using the bitmap resource non-NULL?

    If the returned value from LoadImage is NULL what does GetLastError tell you about why it is NULL.

    Until you do this - or, if you have already tried this - until you post the results of this, there's just going to be a lot of pointless guessing.

    If the return value from LoadImage is non-NULL then create a minimal, compilable example that replicates the problem and post or attach it. This, too, will better help us to help you.
    Ok so again... This code

    Code:
    HBITMAP hCross = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(IDB_CROSS), IMAGE_BITMAP, 13, 13, LR_DEFAULTSIZE);
    Returns NULL even if the appropriate resource exists
    The GetLastError() returns 6 [ERROR_INVALID_HANDLE]

  12. #12
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Looks like the instance handle is incorrect - replace hInstance with GetModuleHandle(0); as the first parameter of LoadImage, assuming that the bitmap is a resource associated with your application instance. If the resource is from a dll or another application, use the instance handle for that instead.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by Ken Fitlike View Post
    Looks like the instance handle is incorrect - replace hInstance with GetModuleHandle(0); as the first parameter of LoadImage, assuming that the bitmap is a resource associated with your application instance. If the resource is from a dll or another application, use the instance handle for that instead.
    The same error (6) as before occures...

  14. #14
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Assuming your GetLastError call was immediately after the LoadImage call then it's time to post that minimum, compilable example that replicates the problem.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  15. #15
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    It works !!!

    Code:
    LoadBitmap(GetModuleHandle(0), MAKEINTRESOURCE(IDB_CROSS));
    This was it... I dont know why it didnt work with my hInstance but it work now...

    Thx very much for your patience
    Last edited by Gordon; 12-22-2007 at 05:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LoadImage() Problem
    By punkrockguy318 in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 08:46 AM
  2. LoadImage() fooled me!
    By Sebastiani in forum Windows Programming
    Replies: 1
    Last Post: 05-07-2002, 10:20 PM
  3. I'm having so much trouble with LoadImage()!!!
    By minesweeper in forum Windows Programming
    Replies: 2
    Last Post: 05-03-2002, 04:28 PM
  4. LoadImage doesn't work when my program is accesing the hardrive
    By pinkcheese in forum Windows Programming
    Replies: 2
    Last Post: 04-20-2002, 12:39 AM
  5. 'The parameter is incorrect' - LoadImage
    By DarkAvenger in forum Windows Programming
    Replies: 1
    Last Post: 04-12-2002, 02:53 PM