1. Home
  2. Computing & Technology
  3. Delphi Programming

Save (and Load) all the Images from a TImageList to a Single File

By , About.com Guide

The WriteComponentResFile Delphi function can be used to store components and their properties to a single file using a resource file format.

Saving Images in a TImageList into a Single File

If you have a TImageList control populated with images, each of the (same sized) images can be accessed using the index of the image.

Images stored in a TImageList are used for different Delphi components like: Menus, TreeViews, ListViewes etc.

If you want to reuse images from a the TImageList control, you can populate the TImageList with images, store all the images in a single resource-like file, then reuse at run time, by dynamically restoring the TImageList control from the saved file.

WriteComponentResFile

To save an instance of a Delphi component and its properties to a file, use the WriteComponentResFile method:
WriteComponentResFile('ImageList.dat', ImageList1) ;
The ImageList1 is the name of the TIMageList component droped on a form and populated with images.

ReadComponentResFile

To read the component and its properties from a file, use the ReadComponentResFile function.

Suppose there were 4 images in the ImageList1 we saved to a file. To restore the image list with its images programmatically, and display the 4 images in 4 TImage controls, you can use the following code:

var
  imageList : TImageList;
begin
  imageList := TImageList.Create(nil) ;
  try
    ReadComponentResFile('ImageList.dat', imageList) ;

    imageList.GetBitmap(0,Image1.Picture.Bitmap) ;
    imageList.GetBitmap(1,Image2.Picture.Bitmap) ;
    imageList.GetBitmap(2,Image3.Picture.Bitmap) ;
    imageList.GetBitmap(3,Image4.Picture.Bitmap) ;
  finally
    FreeAndNil(imageList) ;
  end;
end;

Delphi tips navigator:
» Programmatically Detect the MyDocuments Folder for the Current Windows User using Delphi
« PopupMenu With Different Item Heights (and Custom Graphics) in Delphi Applications

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2007 Tips
  7. Save (and Load) all the Images from a Delphi TImageList to a Single File

©2009 About.com, a part of The New York Times Company.

All rights reserved.