Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Loading Image from file... Which function to use?

Sonx_hvn7Sonx_hvn7 Member Posts: 54
Hi... Tried LoadImage(); but it seems it only for files which are part of resource file (whatever that means).. I'm using the Visual Studio C++ 2005 and would like to load an image from a specified folder???? A code snippet would help.

This is my attempt
[code]
HBITMAP radarBmp = (HBITMAP)LoadImage( NULL,
TEXT("c:\Quest Projects\Visual Studio\Resources\Resources\radar_bgrnd.bmp"), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
[/code]

returns NULL... Error is

[quote]
The specified resource name cannot be found in the image file. ERROR_RESOURCE_NAME_NOT_FOUND
[/quote]

Comments

  • LundinLundin Member Posts: 3,711
    LoadImage will indeed only work with resources. You will need to check up "Device Independent Bitmaps" (DIB). The files are read from the HD as binary files, which you have to put in C structures corresponding to the bitmap file header structure. When you have the DIB in memory, you can put it on the DC with SetDIBitsToDevice().

    There should be plenty of code for this on the net. Otherwise I'd recommend getting the book "Programming Windows" by Petzold.
  • MT2002MT2002 Member Posts: 1,444
    [code]HBITMAP radarBmp = (HBITMAP)LoadImage( NULL,
    TEXT("c:\Quest Projects\Visual Studio\Resources\Resources\radar_bgrnd.bmp"),
    IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);[/code]
    Hm... you *should* be able to load bitmaps from file using LoadImage, just not like the above. (Your above code will indeed attempt to load from a resource)

    Try this instead:

    [code]
    HBITMAP radarBmp = (HBITMAP)LoadImage( NULL,
    TEXT("c:\Quest Projects\Visual Studio\Resources\Resources\radar_bgrnd.bmp"),
    IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    [/code]

    According to MSDN, you do not need to specify LR_DEFAULTCOLOR as it is the default flag.

    Also, I personally use "/" instead of "\" as it seems supported on more systems, and is prettier. This is just my personal taste though ;)

    Last but not least, you should never specify an exact location for a file within a program. That is, the location should be in a configuration file, or a relative path from your programs location.

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : [code]: HBITMAP radarBmp = (HBITMAP)LoadImage( NULL,
    : TEXT("c:\Quest Projects\Visual Studio\Resources\Resources\radar_bgrnd.bmp"),
    : IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);[/code]:
    : Hm... you *should* be able to load bitmaps from file using
    : LoadImage, just not like the above. (Your above code will indeed
    : attempt to load from a resource)
    :
    : Try this instead:
    :
    : [code]:
    : HBITMAP radarBmp = (HBITMAP)LoadImage( NULL,
    : TEXT("c:\Quest Projects\Visual Studio\Resources\Resources\radar_bgrnd.bmp"),
    : IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    : [/code]:
    :
    : According to MSDN, you do not need to specify LR_DEFAULTCOLOR as it
    : is the default flag.
    :
    : Also, I personally use "/" instead of "\" as it seems supported on
    : more systems, and is prettier. This is just my personal taste though
    : ;)
    :
    : Last but not least, you should never specify an exact location for a
    : file within a program. That is, the location should be in a
    : configuration file, or a relative path from your programs location.
    :
    : [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS
    : Operating
    : System[rightbr][leftbr][link=http://www.brokenthorn.com]Website ::
    : OS Development Series[rightbr][/link][/size]
    :
    [color=Blue]The point about hard-coding the path in the code is very wise. It is better to use some kind of relative directory. Something beside your EXE file will do nice. Get the path of your EXE file and search for last directory separator (backslash in your case) - once it is found append at this location some sub-folder: like "Images". Then all your images may be loaded just by name:
    [code]
    LoadMyBitmap ("radar_bgrnd.bmp");
    [/code]
    Internally this function will use the path constructed from path to EXE file. This way - no matter in which directory your program is deployed - all images will be loaded properly.
    [/color]
Sign In or Register to comment.