C/C++ Windows API

Moderators: Lundin
Number of threads: 443
Number of posts: 1215

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Loading Image from file... Which function to use? Posted by Sonx_hvn7 on 25 Apr 2008 at 4:39 AM
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
HBITMAP radarBmp = (HBITMAP)LoadImage( NULL, 
TEXT("c:\\Quest Projects\\Visual Studio\\Resources\\Resources\\radar_bgrnd.bmp"), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);


returns NULL... Error is

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

Report
Re: Loading Image from file... Which function to use? Posted by Lundin on 25 Apr 2008 at 6:17 AM
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.
Report
Re: Loading Image from file... Which function to use? Posted by MT2002 on 25 Apr 2008 at 9:28 AM
HBITMAP radarBmp = (HBITMAP)LoadImage( NULL, 
TEXT("c:\\Quest Projects\\Visual Studio\\Resources\\Resources\\radar_bgrnd.bmp"),
IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);

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:

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


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.


[.:EvolutionEngine][.:MicroOS Operating System][Website :: OS Development Series]
Report
Re: Loading Image from file... Which function to use? Posted by AsmGuru62 on 26 Apr 2008 at 4:09 AM
:
: HBITMAP radarBmp = (HBITMAP)LoadImage( NULL, 
: TEXT("c:\\Quest Projects\\Visual Studio\\Resources\\Resources\\radar_bgrnd.bmp"),
: IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
:
: 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:
:
:
: 
: HBITMAP radarBmp = (HBITMAP)LoadImage( NULL, 
: TEXT("c:\\Quest Projects\\Visual Studio\\Resources\\Resources\\radar_bgrnd.bmp"),
: IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
: 
:
:
: 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.
:
:
[.:EvolutionEngine][.:MicroOS
: Operating
: System][Website ::
: OS Development Series]

:
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:
LoadMyBitmap ("radar_bgrnd.bmp");

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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.