Add Transparent and stretched Image to Imagelist in Delphi
According to my previous question with the help of Cosmin Prund, I found how to stretch Image and add to ImageList:
procedure LoadDatasetImagesToImageList;
var
StretchedBMP: TBitmap;
MS: TMemoryStream;
begin
ImageList.Clear;
ItemsDts.First;
StretchedBMP := TBitmap.Create;
try
// Prepare the stretched bmp's size
StretchedBMP.Width := ImageList.Width;
StretchedBMP.Height := ImageList.Height;
// Prepare the memory stream
MS := TMemoryStream.Create;
try
ImageBitmap:= TBitmap.Create;
try
while not ItemsDts.Eof do
begin
if not ItemsDtsPicture.IsNull then
begin
MS.Size := 0;
ItemsDtsPicture.SaveToStream(MS);
MS.Position := 0;
ImageBitmap.LoadFromStream(MS);
// Stretch the image
StretchedBMP.Canvas.StretchDraw(Rect(0, 0, StretchedBmp.Width-1, StretchedBmp.Height-1), ImageBitmap);
ImageList.Add(StretchedBmp, nil);
end;
ItemsDts.Next;
end;
finally
ImageBitmap.Free;
end;
finally
MS.Free;
end;
finally
StretchedBMP.Free;
end;
Now the problem is that inserted Im开发者_如何学运维age is not transparent in ImageList. When displaying Items in a TListview, images are not transparented. But when adding images normally (without stretching and using StretchedBMP variable) images are transparent.
PS: the link to the previous question is: Add stretched image to ImageList in Delphi
You call ImageList.Add
and pass nil
for the mask image. You can either calculate the mask corresponding to your stretched image, or you can call ImageList.AddMasked
instead to have the image list calculate a mask for you based on a color that you designate as the "transparent" color. That's what happens when you use the image-list component editor at design time.
精彩评论