Read Icon in TAdvTreeView from TImageList

In the manual for TAdvTreeView you discribe how to load an image from a picturecontainer into a row/column using the TVGetIcon event.
I tried to read a png from a TImageLIst but all the methods I tried result into memoryleaks or access violations, because AIcon has to be created. Can you tell me how to read png's from a TImageList and use them in this event?

We recommend to use TPictureContainer, especially for PNG images. If you still wish to use TImageList instead through the OnGetNodeIcon event, you'll need to create a TObjectList of TIcon, filled with content from TImageList. This way you'll be able to pass through the icon in the OnGetNodeIcon event, since the OnGetNodeIcon event only keeps a reference to the image, but does not create nor destroy it. Below is a sample that demonstrates this




unit Unit18;


interface


uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, AdvTreeViewBase, AdvTreeViewData,
  AdvCustomTreeView, AdvTreeView, System.ImageList, Vcl.ImgList, Generics.Collections;


type
  TForm18 = class(TForm)
    AdvTreeView1: TAdvTreeView;
    ImageList1: TImageList;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure AdvTreeView1GetNodeIcon(Sender: TObject;
      ANode: TAdvTreeViewVirtualNode; AColumn: Integer; ALarge: Boolean;
      var AIcon: TGraphic);
  private
    { Private declarations }
    FIcons: TObjectList<TIcon>;
  public
    { Public declarations }
  end;


var
  Form18: TForm18;


implementation


{$R *.dfm}


procedure TForm18.AdvTreeView1GetNodeIcon(Sender: TObject;
  ANode: TAdvTreeViewVirtualNode; AColumn: Integer; ALarge: Boolean;
  var AIcon: TGraphic);
begin
  if Assigned(FIcons) then
    AIcon := FIcons[Random(FIcons.Count)];
end;


procedure TForm18.FormCreate(Sender: TObject);
var
  I: Integer;
  ic: TIcon;
begin
  ReportMemoryLeaksOnShutdown := True;
  FIcons := TObjectList<TIcon>.Create;
  for I := 0 to ImageList1.Count - 1 do
  begin
    ic := TIcon.Create;
    ImageList1.GetIcon(I, ic);
    FIcons.Add(ic);
  end;
end;


procedure TForm18.FormDestroy(Sender: TObject);
begin
  FIcons.Free;
end;


end.

Thank you.
I don't use a TPicturecontainer because it's editor is unusable with more than a few images. See my feature request.