delphitimage

How to insert picture in the Delphi program itself?


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?


Solution

  • 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;