Search code examples
delphidelphi-xe

Add a png image to a imagelist in runtime using Delphi XE


I need add in runtime a png image to a TImageList. I've looked at the functions implemented by the TCustomImageList but they only allow adding

  • bitmaps,
  • icons or
  • images from another imagelist

E.g.:

function Add(Image, Mask: TBitmap): Integer;
function AddIcon(Image: TIcon): Integer;
function AddImage(Value: TCustomImageList; Index: Integer): Integer;
procedure AddImages(Value: TCustomImageList);
function AddMasked(Image: TBitmap; MaskColor: TColor): Integer;

How I can add a PNG image to a ImageList component without converting this image to BMP?

The IDE already can add a PNG to an ImageList at design time:

enter image description here

Now we need to do it at runtime.


Solution

  • According to MSDN an imagelist can only contain bitmaps and icons. To add a png image to an imagelist you have to convert it to an icon first. The code to do that can be found in the PngComponents package. If you have only PNG images in your imagelist you can for simplicity just use TPngImageList that comes with that package.