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
Delphi

Avatar of undefined
Last Comment
Geert G

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ephraim Wangoya

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Geert G

images in a imagelist all have to be the same size for width and height
are they all the same size ?
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?
Ephraim Wangoya

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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Ephraim Wangoya

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

Member_2_99151

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

James
Geert G

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.