Populate an ImageList at runtime in Delphi (2010)

Member_2_99151
Member_2_99151 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer
Commented:
First thing, just call ImageList.Clear, there is no need to specifically destroy the images

Second you will have a huge memory leak, so dont create the bitmaps when adding to the imagelist

//Clean up existing
myImageList.Clear;

// Now add them
for I := 0 to myObjectWithBitmapArray.Count - 1 do
    myImageList.Add(TObjectWithBitmap(myObjectWithBitmapArray.Items[I]).myBitmap, nil);

PictureComboBox1.ImageList := myImageList;
//invalidate is just for redrawing when the opportunity arises, no need to call it    

Open in new window


I dont know exactly how the PictureCombobox works, If this doed not work then you need to investigate your a array, What size of bitmap does the image list expect and is it the correct size in the array
Geert GOracle dba
Top Expert 2009

Commented:
images in a imagelist all have to be the same size for width and height
are they all the same size ?

Author

Commented:
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?
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Ephraim WangoyaSoftware Engineer

Commented:
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

Ephraim WangoyaSoftware Engineer

Commented:
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

Author

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

James
Geert GOracle dba
Top Expert 2009

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial