在Delphi中将透明和拉伸的图像添加到图像列表中

时间:2011-07-14 04:05:18

标签: image delphi transparent stretch imagelist

根据我在Cosmin Prund帮助下的上一个问题,我找到了如何拉伸Image并添加到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;

现在问题是插入的Image在ImageList中不透明。在TListview中显示项目时,图像不会透明。 但是在正常添加图像时(没有拉伸和使用StretchedBMP变量)图像是透明的。

PS:上一个问题的链接是:Add stretched image to ImageList in Delphi

1 个答案:

答案 0 :(得分:10)

您致电ImageList.Add并传递nil作为掩码图片。您可以计算与拉伸图像对应的蒙版,也可以调用ImageList.AddMasked来让图像列表根据您指定为“透明”颜色的颜色为您计算蒙版。这就是在设计时使用图像列表组件编辑器时会发生的情况。

相关问题