Placing an image file in an executable in C


If you ever wanted to place an image file into the executable or store any resources in it, this post will be able to help you.  If you have previously read Builder & Stub | How to create your own builder and stub in C (using Resource), you will be able to understand this post easily.  We are using the similar method by placing the image in the resource data.  In this post, i am creating an application that extracts the image that has been placed in the resource data and place it in a file and execute the file. 

Prepare a project called PictureExtract (I am using Microsoft Visual C++ 6.0 for this example).  As usual, you will have to prepare your main file and your function main or WinMain (I am using a creating a console application in this case).  Now we are required to create a resource script whereby we can place our picture in the resource data.

Now that you have a resource.rc file in your project we will place an image in the resource.  Go to Resource View and right-click on resource.rc, select Add Resource….  Press Import.  When the window to browse files appear, make sure you select All Files (*.*) under Files of type so that you are able to select any file extension.  Select any picture image filename (the filename i have will be picture.png). When it ask for the Custom Resource Type, enter IMAGE.

When you press enter you would now have something appeared under your resources tree.

Now that the general preparations are done all that is left is coding.  Go to your main file and paste the code below.

Source Code

Before you use the source code below, you have to ensure that you have already inserted an image into the resource with the Custom Resource Type name as IMAGE. Make sure you follow the instructions above on how to insert an image into the resource.

Download

If you are not still not sure what to do, i have uploaded the project. Download project here.

/*
 * This example was made by GenesisDatabase.
 * Visit https://genesisdatabase.wordpress.com for more source codes!
 *
 * Date of release: 17th January 2011
 */

#include
#include
#include "resource.h"

int main()
{
	HRSRC ResourceLocation = NULL;
	LPSTR ResourcePointer = NULL;
	FILE *write = NULL;
	unsigned int ResourceSize = 0;

	// get handle to image location
	ResourceLocation = FindResource(NULL,  MAKEINTRESOURCE(IDR_IMAGE1), "IMAGE");
	if(ResourceLocation == NULL)
	{
		printf("Error 1\n");
		getchar();
		return 0;
	}

	// get size of image
	ResourceSize = SizeofResource(NULL, ResourceLocation);
	if(ResourceSize == 0)
	{
		printf("Error 2\n");
		getchar();
		return 0;
	}

	// get pointer to image
	ResourcePointer = (LPSTR)LoadResource(NULL, ResourceLocation);
	if(ResourcePointer == NULL)
	{
		printf("Error 3\n");
		getchar();
		return 0;
	}

	// at this point we have already retrieved the image file in buffer

	// get handle to a file we going to place the buffer in
	write = fopen("picture.png", "wb");
	if(write == NULL)
	{
		printf("Error 4\n");
		getchar();
		return 0;
	}

	// write buffer into file
	if(fwrite(ResourcePointer, 1, ResourceSize, write) < ResourceSize)
	{
		printf("Error 5\n");
		getchar();
		return 0;
	}

	// close file handle
	if(fclose(write) != 0)
	{
		printf("Error 6\n");
		getchar();
		return 0;
	}

	// execute using default picture viewer
	ShellExecute(NULL, NULL, "picture.png", NULL, NULL, SW_SHOW);

	printf("Success.\n");
	getchar();
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: