Can You Pack Images Into an EXE?

Started by
4 comments, last by GameDev.net 17 years, 11 months ago
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?
Advertisement
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

[edit]
Found these usefull links:
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
[edit]

Hope that helps!

[Edited by - RaptorZero on April 22, 2006 9:41:13 AM]
error C2065: 'signature' : undeclared identifier
Quote:Original post by RaptorZeroNote: assumed you are using C++:

and Windows.

I'd be using Allegro, in this case.
Quote:Original post by Grantyt3
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?
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).
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.

This topic is closed to new replies.

Advertisement