How to insert picture in the Delphi program itself?

12,458

Solution 1

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;

Solution 2

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,

  • select the Picture property
  • click the ... button to open the Picture Editor
  • press the Load... button and select the image file
  • Close the editor with the OK button.

If you want to load the image in code instead, simply do as Remy showed.

Share:
12,458
Admin
Author by

Admin

Updated on June 21, 2022

Comments

  • Admin
    Admin almost 2 years

    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?