如何从TImageList拍摄图片并将其放入TImage(或以TGraphic形式返回)?

重要的一点是TImageList可以包含32bpp的alpha混合图像。目的是获得这些alpha混合图像之一,并将其​​放置在TImage中。这意味着在某个时候我可能需要TGraphic。尽管严格来说,我的问题是将图像从 ImageList 放入图像。如果没有中介的TGraphic就可以实现,那也很好。

我们想要什么?

我们想要一个函数的胆量:

procedure GetImageListImageIntoImage(SourceImageList: TCustomImageList;
      ImageIndex: Integer; TargetImage: TImage);
begin
   //TODO: Figure this out.
   //Neither SourceImageList.GetIcon nor SourceImageList.GetBitmap preserve the alpha channel
end;

还可以使用另一个有用的中间帮助器功能:
function ImageListGetGraphic(ImageList: TCustomImageList; ImageIndex: Integer): TGraphic;
var
//  ico: TIcon;
    bmp: TBitmap;
begin
    {Doesn't work; loses alpha channel.
    Windows Icon format can support 32bpp alpha bitmaps. But it just doesn't work here
    ico := TIcon.Create;
    ImageList.GetIcon(ImageIndex, ico, dsTransparent, itImage);
    Result := ico;
    }

    {Doesn't work; loses alpha channel.
    Windows does support 32bpp alpha bitmaps. But it just doesn't work here
    bmp := TBitmap.Create;
    bmp.PixelFormat := pf32bit;
    Imagelist.GetBitmap(ImageIndex, bmp);
    Result := bmp;
    }
end;

让我们将原始过程转换为:
procedure GetImageListImageIntoImage(SourceImageList: TCustomImageList; ImageIndex: Integer; TargetImage: TImage);
var
   g: TGraphic;
begin
   g := ImageListGetGraphic(SourceImageList, ImageIndex);
   TargetImage.Picture.Graphic := g; //Assignment of TGraphic does a copy
   g.Free;
end;

我也有一些随机的东西:
Image1.Picture := TPicture(ImageList1.Components[0]);

但这不能编译。

附言我有Delphi 2010

最佳答案

ImageList1.GetBitmap(0, Image1.Picture.Bitmap);

08-26 00:28