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
Printable View
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
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
Hi Mats, this is what i got from MSDN
This doesn't match the example you gave, please correct me if am wrong...Code:HRESULT LoadImage(LPWSTR pszFileName,
IUnknown *pDirectDraw,
DDSURFACEDESC *pDDSurfaceDesc,
GUID *pFormatID,
REFIID riid,
void **ppDXSurface
);
When you copy the path provided to the start\run menu is the file opened without problems?
Quote:
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
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
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
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.Quote:
Originally Posted by msdn, LoadImage
moved the file to the c: drive, and changed the code with no avail...
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 saysCode:radarBmp = (HBITMAP)LoadImage( NULL, TEXT("c:\\radar_bgrnd.bmp"),
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
.. Why is it saying unused as if LoadImage() never assigned any value... Maybe the casting (HBITMAP) is not workingCode:0x00000000 {unused=???}
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
GetLastError() is convinced the image was loaded successfully cause it returns 0, but hndl recieves a NULL ... confusing!!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);
}
BTW, why does MessageBox sends about 6 "File Error" message before the window closes?
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;
}