2

I'm pretty new to Windows programming and have been following theForger's Win32 API Programming Tutorial. I've been trying to draw an image inside a window.

Having looked at similar problems, this code seems to be correct for loading a bitmap:

HBITMAP testImage == NULL;
case WM_CREATE:
    testImage = (HBITMAP)LoadImage(NULL, L"C:\\ScreenSnip.bmp", IMAGE_BITMAP, 498, 304, LR_LOADFROMFILE);
    if(testImage == NULL) {
        MessageBox(NULL, L"NO IMAGE LOADED!", L"Error!",  MB_ICONEXCLAMATION | MB_OK);
    }
break;

I have an image called ScreenSnip.bmp at the location above, and its dimensions are 498*304. However, LoadImage doesn't work and the value of testImage is always null.

I have tried loading an image as a resource using LoadBitmap() and that works, which is why I haven't included the rest of my code. It seems to be LoadImage() above that's causing the problem, but I can't figure out why.

Anyone have any ideas? I'm running this using VC++ on Windows 7 64bit.

BeneGal
  • 180
  • 1
  • 11
  • Have you tried with `cxDesired` and `cyDesired` set to zero? There's no indication in the docs that these "desired size" arguments are allowed for anything other than icon or cursor. – Anton Kovalenko Jan 24 '13 at 17:47
  • I have tried that, it hasn't made any difference I'm afraid! – BeneGal Jan 24 '13 at 17:54
  • 4
    *If the function fails, the return value is NULL. To get extended error information, call GetLastError.* – chris Jan 24 '13 at 17:56
  • 1
    Thanks @chris - I used GetLastError which was giving me a value of 0. I found [this question](http://stackoverflow.com/questions/6242193/loadimage-returns-null-and-getlasterror-returns-0) and I think the suggestions there might be exactly what I'm looking for. Thanks for your help! – BeneGal Jan 24 '13 at 18:05
  • 1
    LoadImage api works well only with un-compressed bitmap file, check whether the bitmap file that you intended to load is not compressed with RLE encoding. – mfc Jan 24 '13 at 20:28
  • 1
    Did you call GetLastError immediately after LoadImage or did you MessageBox first? – Ben Jan 24 '13 at 20:47
  • Try locating the file in a user folder. Vista and later got finicky about files located in C:\ for security reasons. For example, I can't save a file there is MS Paint unless I'm elevated to administrator. So you're probably just having an access rights issue. – Adrian McCarthy Jan 24 '13 at 21:20
  • Make a simple bitmap in MS Paint and try loading that. If it works, then there's probably something funky about your ScreenSnip.bmp. – Adrian McCarthy Jan 24 '13 at 21:34

1 Answers1

-1

use testImage = (HBITMAP)LoadImage(NULL, L"C:\\ScreenSnip.bmp", IMAGE_BITMAP, 498, 304, LR_LOADFROMFILE); you use wide wiht L"C:.." so you have to use LoadImageW

Carl Mark
  • 371
  • 1
  • 12
  • 23