'how to access image picture in a TImageCollection in Delphi 11
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.
Solution 1:[1]
First, consider Uwe Raabe's answer and understand the the purpose of TImageCollection. Maybe you simply need a TImageList component instead.
ImageCollection.GetBitmap() first looks for an exact size match. If not found, it creates a scaled bitmap according to the requested size by picking the best source.
If you still need to access a specific source image by its index, it could be done like this:
// 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 in some very specific cases.
Solution 2:[2]
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;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Remy Lebeau |
| Solution 2 | Uwe Raabe |
