2

根据我之前在 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

4

1 回答 1

10

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

于 2011-07-14T04:19:53.827 回答