Link to home
Start Free TrialLog in
Avatar of Member_2_99151
Member_2_99151

asked on

Populate an ImageList at runtime in Delphi (2010)

Hi all,

I have an app written in Delphi and I wish to do the following:

1. Add images dynamically to an ImageList
2. Display the contents of this list in a combo box.

This list may change as the program executes, so this needs to be dynamic.

At the moment I am using a "TPictureComboBox" by DarkAges to display the images and a standard ImageList to store the images.

I have tried the following to no avail....
    // Clean up existing
    while myImageList.Count > 0 do
    begin
        myImageList.GetBitmap(0, xBitmap);
        xBitmap.Free;
        myImageList.Delete(0);
    end;
    myImageList.Clear;

    // Now add them
    for I := 0 to myObjectWithBitmapArray.Count - 1 do
    begin
        xBitmap := TBitmap.Create;
        xBitmap.Assign(TObjectWithBitmap(myObjectWithBitmapArray.Items[I]).myBitmap);
        myImageList.Add(xBitmap, nil);
    end;
    PictureComboBox1.ImageList := myImageList;
    PictureComboBox1.Invalidate;
    PictureComboBox1.Refresh;

Open in new window


But I just get an empty list :-(

Any ideas what I'm missing?

Thanks,

James
ASKER CERTIFIED SOLUTION
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
images in a imagelist all have to be the same size for width and height
are they all the same size ?
Avatar of Member_2_99151
Member_2_99151

ASKER

Hi,

Thanks for the feedback - it looks like the control itself is actually misbehaving itself!!
I have changed this to TLMDImageComboBox and things appear to be much better :-)

Regarding the creation of the bitmap, the image stored in the class is actually a TJPGImage, so I was creating a TBitmap and using 'Assign' to do the conversion.

Is there a more 'correct' way to do this?
in that case, make sure you destroy it as follows

for I := 0 to myObjectWithBitmapArray.Count - 1 do
    begin
        xBitmap := TBitmap.Create;
       try
          Bitmap.Assign(TObjectWithBitmap(myObjectWithBitmapArray.Items[I]).myBitmap);
          myImageList.Add(xBitmap, nil);
       finally
         FreeAndNil(xBitmap );
       end;
    end;

Open in new window

Typo correction

    for I := 0 to myObjectWithBitmapArray.Count - 1 do
    begin
        xBitmap := TBitmap.Create;
       try
          xBitmap.Assign(TObjectWithBitmap(myObjectWithBitmapArray.Items[I]).myBitmap);
          myImageList.Add(xBitmap, nil);
       finally
         FreeAndNil(xBitmap );
       end;
    end;

Open in new window

Ah, I didn't realise that the .Add method created a copy of the image!
Thanks for the help :-)

James
if it works it's 'correct', but not always the best way for performance

> you could add a caching system to cache the last 50 or so images in memory