[英]Add a png image to a imagelist in runtime using Delphi XE
我需要在运行时将 png 图像添加到TImageList
。 我查看了TCustomImageList
实现的功能,但它们只允许添加
例如:
function Add(Image, Mask: TBitmap): Integer;
function AddIcon(Image: TIcon): Integer;
function AddImage(Value: TCustomImageList; Index: Integer): Integer;
procedure AddImages(Value: TCustomImageList);
function AddMasked(Image: TBitmap; MaskColor: TColor): Integer;
如何在不将此图像转换为 BMP 的情况下将 PNG 图像添加到 ImageList 组件?
IDE 已经可以在设计时将 PNG 添加到 ImageList:
现在我们需要在运行时进行。
Delphi XE 完全支持处理带有 alpha 通道的 png 图像和 32 位位图。 以下是将 png 添加到 ImageList 的方法:
uses CommCtrl;
var pngbmp: TPngImage;
bmp: TBitmap;
ImageList: TImageList;
begin
ImageList:=TImageList.Create(Self);
ImageList.Masked:=false;
ImageList.ColorDepth:=cd32bit;
pngbmp:=TPNGImage.Create;
pngbmp.LoadFromFile('test.png');
bmp:=TBitmap.Create;
pngbmp.AssignTo(bmp);
// ====================================================
// Important or else it gets alpha blended into the list! After Assign
// AlphaFormat is afDefined which is OK if you want to draw 32 bit bmp
// with alpha blending on a canvas but not OK if you put it into
// ImageList -- it will be way too dark!
// ====================================================
bmp.AlphaFormat:=afIgnored;
ImageList_Add(ImageList.Handle, bmp.Handle, 0);
你必须包括
ImgList, PngImage
如果您现在尝试:
Pngbmp.Draw(Bmp1.Canvas,Rect);
and
ImageList.Draw(Bmp1.Canvas,0,0,0,true);
你会看到图像是一样的。 实际上,由于 alpha 混合过程中的舍入误差,存在一些 \\pm 1 rgb 差异,但您无法用肉眼看到它们。 忽略设置 bmp.AlphaFormat:=afIgnored; 会导致第二个图像更暗!
此致,
亚历克斯
根据 MSDN,图像列表只能包含位图和图标。 要将 png 图像添加到图像列表,您必须先将其转换为图标。 可以在PngComponents包中找到执行此操作的代码。 如果您的图像列表中只有 PNG 图像,为简单起见,您可以使用该包附带的 TPngImageList。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.