Go Premium for a chance to win a PS4. Enter to Win


add a resource file into your program

Posted on 2003-11-01
Medium Priority
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

ID: 9662553

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

ID: 9662556

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

ID: 9663672
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 http://www.torry.net/vcl/vcltools/codertools/exemod.zip
The version of exemod.pas in the zip is out of date you can find the newer one at my website http://www.geocities.com/gacarpenter386/

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

Ok..now 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 messages...so 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 :-)
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 9674673
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.

Author Comment

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

Author Comment

ID: 10746484
Can someone delete this question and refund the pts.?

Accepted Solution

StTwister earned 260 total points
ID: 10787353
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

ID: 10798592
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Screencast - Getting to Know the Pipeline

926 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