I have used a TImage
component in my program.
At runtime, I add an image to the component by using:
Image1.Picture.LoadFromFile('C:\Users\53941\Pictures\eq1.jpg');
Now, I want to run this program on some other computer, which would not have this image file at the source I have given in the code.
So, how can I store this image file in the program executable itself?
Insert Image dialog boxChoose Insert > Image > From File from the Menu bar. On the Insert Image dialog, navigate to the file to be inserted, and select it. At the bottom of the dialog are two options, Preview and Link.
Select the Insert tab. Click the Picture command in the Illustrations group. The Insert Picture dialog box appears. Select the desired image file, then click Insert to add it to your document.
Store the image file in the program's resources, using an .rc
script or the IDE's Resources and Images
dialog. Read Embarcadero's documentation for more details:
Resources and Images
You can then use a TResourceStream
to access the resource data at runtime. Construct a TJPEGImage
object, load the stream into it, and assign it to the TImage
:
uses
..., Classes, Jpeg;
var
Strm: TResourceStream;
Jpg: TJPEGImage;
begin
Strm := TResourceStream.Create(HInstance, '<Resource identifier>', RT_RCDATA);
try
Jpg := TJPEGImage.Create;
try
Jpg.LoadFromStream(Strm);
Image1.Picture.Assign(Jpg);
finally
Jpg.Free;
end;
finally
Strm.Free;
end;
end;
You can assign the image using the Object Inspector when designing the Form. The image will be stored in the Form's DFM and be loaded automatically when the program runs.
Select the TImage
and, in the Object Inspector,
Picture
propertyIf you want to load the image in code instead, simply do as Remy showed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With