Home > Enterprise >  how to access image picture in a TImageCollection in Delphi 11
how to access image picture in a TImageCollection in Delphi 11

Time:04-26

I've loaded several images in a TImageCollection. I want to access the pictures inside to copy to a fastreport and alike. I've tried something like this:

var
  vi_imagen :TImageCollectionSourceItem;
  bmp: tbitmap;
  MyPicture: TfrxPictureView;
begin
    vi_imagen := imagecollection.images[1].sourceimages[0];
    bmp.Assign(vi_imagen); 

    MyPicture.Picture.Assign(bmp); 
end;

How can I do it? Thanks.

CodePudding user response:

The main purpose of TImageCollection is to provide images in different sizes. For that it offers methods to retrieve an image of a specific size. The simplest is GetBitmap, which takes the Index of the image and the requested size.

var
  bmp: TBitmap;
begin
  bmp := imagecollection.GetBitmap(1, 48, 48); // get 48 pixel 
  try
    MyPicture.Picture.Assign(bmp); 
  finally
    bmp.Free;
  end;
end;

CodePudding user response:

First, consider Uwe Raabe's answer and understand the the purpose of the TImageCollection. Maybe You simply need a TImageList component.

ImageCollection.GetBitmap() looks first for exact size match. If not found, it creates a scaled bitmap according to the asked size by picking the best source.

If You still need to access specific source image by it's index, it could be done like:

// Assign to a TPicture instance
Image.Picture.Assign(ImageCollection.Images[X].SourceImages[Y].Image);
// Or assign to a TBitmap instance
Bitmap.Assign(ImageCollection.Images[X].SourceImages[Y].Image);

Normally, You should not use this approach, except some very specific cases.

  • Related