add a resource file into your program

Hi, how do you put a resource file into your program, like a .jpg file, and drop it ?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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.



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.


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 :-)
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

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.
herd_boneAuthor Commented:
Sorry, i meant to say i need to add a compiled .exe into my program and install it in a directory.
herd_boneAuthor Commented:
Can someone delete this question and refund the pts.?
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;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
herd_boneAuthor Commented:
Thanks StTwister, this question is pretty old , i figured out how to do it but they didnt delete the question ands refund the points
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.