Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert picture in the Delphi program itself?

Tags:

delphi

timage

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?

like image 472
Aryaman Bansal Avatar asked Jul 25 '17 17:07

Aryaman Bansal


People also ask

How do you insert and use a picture?

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.

Which command is used to insert a picture?

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.


2 Answers

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;
like image 189
Remy Lebeau Avatar answered Nov 01 '22 20:11

Remy Lebeau


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.

like image 27
Rudy Velthuis Avatar answered Nov 01 '22 20:11

Rudy Velthuis