I was in need to load the images for a TImageList (in this example TPngImageList) from the file system at runtime. To prevent reinventing the stupid work every time I just created a class helper. Here’s the code:
unit UPngImageHelper; interface uses PngImageList; type TPngImageListHelper = class helper for TPngImageList function AddPngFromFile(AFileName: String):integer; end; implementation uses System.SysUtils, PngImage; { TPngImageListHelper } function TPngImageListHelper.AddPngFromFile(AFileName: String):integer; var png: TPngImage; begin png := TPngImage.Create; try png.LoadFromFile(AFileName); Result := self.AddPng(png); finally FreeAndNil(png); end; end; end.
Usage:
uses UPngImageHelper; //... procedure TfrmMain.InitializeToolbar; begin PngImageList1.Clear; PngImageList1.Width := 48; PngImageList1.Height := 48; Action1.ImageIndex:=PngImageList1.AddPngFromFile('images\1.png'); ToolButton1.Action:=Action1; Action2.ImageIndex:=PngImageList1.AddPngFromFile('images\2.png'); ToolButton2.Action:=Action2; Action3.ImageIndex:=PngImageList1.AddPngFromFile('images\3.png'); ToolButton3.Action:=Action3; Action4.ImageIndex:=PngImageList1.AddPngFromFile('images\4.png'); ToolButton4.Action:=Action4; end;
Quick-Tipp: Load Images from file into ImageList