如何从 a 中拍照TImageList
并将其放入 a TImage
(或将其返回为 a TGraphic
)?
重要的一点是 aTImageList
可以包含 32-bpp alpha 混合图像。目标是获取这些 alpha 混合图像之一并将其放置在TImage
. 这意味着在某些时候我可能需要一个TGraphic
. 虽然严格来说,我的问题是将ImageList中的图像放入Image。如果这可以在没有中间体的情况下完成,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]);
但这不会编译。
PS我有德尔福2010