How To Include JPEG's In Your Executable (Delphi 3)
From: Marko Peric
I'm a Delphi beginner who's exploring this wonderful programming environment,
and I just thought I'd like to share some really neat stuff I worked out.
Delphi3 comes with the jpeg unit, and that's great, but did you ever wonder
how to include jpeg's in your executable and then use them in your application?
Well, follow this simple 5 step plan and you can't go wrong!
STEP ONE:
Create a resource script file (*.RC) with a simple text editor like Notepad and add the following line:
--------------------------------------------------------------------------------
1 RCDATA "MyPic.jpg"
--------------------------------------------------------------------------------
The first entry is simply the index of the resource. The second entry specifies that we are dealing
with a user-defined resource. The third and final entry is the name of the jpeg file.
STEP TWO:
Use Borland's Resource Compiler, BRCC32.EXE, to compile it into a .RES file. At the MS-DOS command line:
--------------------------------------------------------------------------------
BRCC32 MyPic.RC
--------------------------------------------------------------------------------
This will create a resource file called MyPic.RES.
STEP THREE:
Add a compiler directive to the source code of your program. It should immediately
follow the form directive, as shown here:
--------------------------------------------------------------------------------
{$R *.DFM}
{$R MyPic.RES}
--------------------------------------------------------------------------------
STEP FOUR:
Add the following code to your project (I've created a procedure for it):
--------------------------------------------------------------------------------
procedure LoadJPEGfromEXE;
var
MyJPG : TJPEGImage; // JPEG object
ResStream : TResourceStream; // Resource Stream object
begin
try
MyJPG := TJPEGImage.Create;
ResStream := TResourceStream.CreateFromID(HInstance, 1, RT_RCDATA);
MyJPG.LoadFromStream(ResStream); // What!? Yes, that easy!
Canvas.Draw(12,12,MyJPG); // draw it to see if it really worked!
finally
MyJPG.Free;
ResStream.Free;
end;
end; // procedure
--------------------------------------------------------------------------------
See the second parameter of the CreateFromID procedure of the TResourceStream component?
It's simply the resource index. You can include more than one jpeg in your executable just
by adding a line for each jpeg (with a different index) in the resource script (.RC) file.
STEP FIVE:
Call the procedure, run the program, and voila! Now go eat some nachos.