Can You Pack Images Into an EXE?

This topic is 4326 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Is it possible to pack the images used in a program into the executable file at compile-time so that all the needed files are contained in the exe?

Share on other sites
Yes
You can put them in the executable resources.

Note: assumed you are using C++:
resource.h
#define ID_BITMAP_RESOURCE 1001

resource.rc
ID_BITMAP_RESOURCE BITMAP DISCARDABLE "bitmap.bmp"

*.cpp
HBITMAP hBMP;BITMAP	BMP;	hBMP = (HBITMAP)LoadImage(GetModuleHandle(null), MAKEINTRESOURCE(ID_BITMAP_RESOURCE), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);if(!hBMP)	return false;// You can cut this out if you want to use the bitmap for Win32 stuff since you only need the hBMPGetObject(hBMP, sizeof(BITMAP), &BMP);// Remember to free these resources after ther're not needed anymore

That code should load a resource bitmap into BMP, take a look at the BITMAP structure for more info

http://www.emmestech.com/colin_peters_tutorial/resources/bitmap.html
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_2h6a.asp

Hope that helps!

[Edited by - RaptorZero on April 22, 2006 9:41:13 AM]

Share on other sites
Quote:
 Original post by RaptorZeroNote: assumed you are using C++:

and Windows.

Share on other sites
I'd be using Allegro, in this case.

Share on other sites
Quote:
 Original post by Grantyt3Is it possible to pack the images used in a program into the executable file at compile-time so that all the needed files are contained in the exe?
An easy and portable way is to write a small script which converts an image file's contents into a string like this:

static const unsigned char* myImage="\x03\x53\x2A\x5F...";

And copy this line into your program. Then pass myImage to whatever image loading function you have. I'm not 100% sure on this, but I think it'll be equally fast and essentially use equal amount of memory as any other solution, on current OSs (the array will be loaded from disk to memory only once it is accessed, and if you run out of memory the pages with the image-array will just be dropped--it's already in the disk anyway if needed again later).

Share on other sites
Don't over do it though. Resources increase the amount of time it takes for your program to load, and they also increase the memory footprint of your exe file, once loaded.