How to insert a picture into a Delphi program?
I have used a component TImage
in my program.
At runtime, I add an image to the component using:
Image1.Picture.LoadFromFile('C:\Users\53941\Pictures\eq1.jpg');
Now I want to run this program on another computer that does not have this image file in the source specified in the code.
So how can I save this image file in the executable software itself?
source to share
Save the image file in program resources using .rc
script dialog or IDE Resources and Images
. Read the Embarcadero documentation for more details:
Then you can use TResourceStream
to access the resource data at runtime. Build an object TJPEGImage
, load a stream into it and assign it 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;
source to share
You can assign an image using the Object inspector when designing a shape. The image will be saved in DFM form and loaded automatically when the program starts.
Select TImage
and, in the Object inspector,
- select property
Picture
- click the button ...to open the Image Editor
- click the button Load...and select the image file
- Close the editor with the button OK .
If you want to load the image in code instead, just do as Remy showed.
source to share