I am having difficulties with the LoadImage() function
This is how I use it...
(The Drawstate works ok...)
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 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...
Shouldn't '101' be IDI_MYIMAGE (or whatever the assigned name may be)?
Yes it could, but its not necessary...
Originally Posted by Oldman47
No, it's not necessary, but it makes it easier to read the code half a year later.
Originally Posted by Gordon
What does LoadImage() return? ie a valid HBITMAP?
If LoadImage() fails, what does GetLastError() return?
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
Originally Posted by novacain
If I use this code
Does not work either
HBITMAP hCross = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(IDB_CROSS), IMAGE_BITMAP, 15, 15, LR_DEFAULTSIZE);
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".
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.
I have done a few investigations about this issue...
I tried to create a brand new project
In each case of drawing I use only one line either blue or red... The blue one works well... The red does not work...
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);
BitBlt(hDC, 100, 100, 800, 600, hOffDC, 0, 0, SRCCOPY);
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.
Could you please follow the advice given to you twice in this thread and post the results of that?
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
Originally Posted by Ken Fitlike
Returns NULL even if the appropriate resource exists
HBITMAP hCross = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(IDB_CROSS), IMAGE_BITMAP, 13, 13, LR_DEFAULTSIZE);
The GetLastError() returns 6 [ERROR_INVALID_HANDLE]
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...
Originally Posted by Ken Fitlike
Assuming your GetLastError call was immediately after the LoadImage call then it's time to post that minimum, compilable example that replicates the problem.
It works !!!
This was it... I dont know why it didnt work with my hInstance but it work now...
Thx very much for your patience