add a resource file into your program

Posted on 2003-11-01
Last Modified: 2010-04-05
Hi, how do you put a resource file into your program, like a .jpg file, and drop it ?
Question by:herd_bone

Expert Comment

Comment Utility

This is quite simple and you can find lots of web pages speaking of this.
I would anyway like to point you to a class in the VCL : TResourceStream.

Have a look at it and have fun.



Expert Comment

Comment Utility

I was forgetting! ^_^

If you mean to embed a compiled resource into the executable,  use the
{$R FileName.Res} directive! ^_^

Remember that DFM files are basically resources with the
name changed.



Expert Comment

Comment Utility
It sounds like you want to be able to just drag a jpg onto your running exe and have the jpg added to the exe?

If that is right then I have a demo that does that... it is called jpgviewer, it is in some demos in a zip for a unit
called exemod.pas. You can download it here
The version of exemod.pas in the zip is out of date you can find the newer one at my website

here is the readme for this demo

This demo creates stand-alone jpg image viewers...

When you start it up it displays a tiny form with 3 buttons..

To add a jpg file to your jpg archive/viewer just click on the top button
---->[Add A New Jpg To Your Image Viewer]

OR better yet just drag and drop up to 1000 jpg's at a time onto the form....

Once you have added all your images you have a choice to make...

You can rebuild the current exe and change it into the new viewer
by clicking --> [Create An Image Viewer by Rebuilding This Exe]
(this is not the best course of action.. I just included this option
to show how to alter the current exe into the final at runtime)

Or you can click --> [Eject new Viewer App In This Dir as -> View.exe]
This will create a new stand-alone jpg viewer named View.exe in the current
directory. that you have the new viewer built just run it and the first image
will display full screen... the aspect ratio will remain correct for each image.
Just press the Backspace key to move backwards through your archive of images
and press the space-bar to move forward... clicking the left mouse button on the image
will also move forward through the pictures... to terminate the app just press the
escape key...

As this is just a simple demo I did not resample the images when converting to
the full screen size... adding resampling code would enhance the images quite a bit!

I also did not handle the jpg error if you add a bad jpg to the archive
up will pop an annoying error message when you try to view it :-(  this also happens if
you accidently add a non-jpg file to the archive.  It would be a simple matter to add
a test to weed out the bad images before they were added to the archive... but I was in
a rush to get this demo out :-)

Even though I set the max number of images at 1000 It is NOT such a good idea to
create a stand-alone viewer with that many pix in it!  It just gets too huge and takes
too long to compile the thing :-(   try and stay at <100 or so and you will have
better results.  I did make a huge 1000 pic viewer as a test... it ended up at 86mb!
.. it took about 3 minutes to compile on a Athlon 1600+ with 256mb ram... but it was
useable even on an old K6-2-500mhz with 64mb ram... although I would have hated to
wait for it to compile on that slow box ;-)

This simple demo could quickly be altered into many useful little programs...


p.s. Use this code in any way you like... I consider it public domain... like all the rest of the strange software I write :-)

Expert Comment

Comment Utility
if you want to add a simgle picture to your project, the most simple way is to use a TImage component, and load the picture (bmp, jpg, ...) into it. and when you want to access it, that here:

image1.picture. ....

it's so simple and usefull for small size JPEGS.
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.


Author Comment

Comment Utility
Sorry, i meant to say i need to add a compiled .exe into my program and install it in a directory.

Author Comment

Comment Utility
Can someone delete this question and refund the pts.?

Accepted Solution

StTwister earned 65 total points
Comment Utility
Embedding files



Delphi offers a great way to store any tipe of file (avi, wav, EXE etc.) in the application (executable) by supporting resource files.

There are few steps to be done:

Creating a resource script (RC) file
Create a file MyRes.rc. In this file put the following line:
xx wave MyFile.wav

In case of adding more files, the line above has to be repeated.
xx represents the identifier for the embedded file (an identifier, that can be any name, must be put for each file) and wave represents the file type. As it can be seen, the line format is:

[resourcename] [filetype] [filename]

and in case the type of the file isn't known, filetype can be replaced with RCDATA.

Compiling MyRes.rc
Compile the RC file to a RES file by typing in the command prompt:

brcc32 MyRes.rc

After this will result MyRes.res - the resource file.

Including MyRes.res file in the project
The resource file must be mentioned in the *.dpr file or in the *.pas file, under implementation as it can be seen below:

{$R *.dfm}
{$R MyRes.res}

Playing the wave
For playing the wave from the embedded file, the function below can be used:
PlaySound('xx',hInstance,SND_RESOURCE or SND_SYNC);


Embedding executables

As I said earlier, it is possible to store anything in the exe, even another exe! You have to follow the same steps as for the wave file (see above), the single difference being that the file type is exefile, not wave, so the line in the resource script file will be:

q exefile MyExe.exe,

where q is the identifier.
A reason for putting an exe in another is to execute it while running the main or just extracting it on the hard disk for a later action ;-). For extracting the embedded file on the hard disk it can be used a procedure like this:

procedure TForm1.ExtractRes(ResType,ResName,
var ExeRes:TResourceStream;

And here is an example for calling the procedure ExtractRes:

procedure TForm1.FormCreate(Sender: TObject);
var SysDir:Array[0..Max_Path] of Char;


Author Comment

Comment Utility
Thanks StTwister, this question is pretty old , i figured out how to do it but they didnt delete the question ands refund the points

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now