Add Transparent and stretched Image to Imagelist in Delphi

ぐ巨炮叔叔 提交于 2019-12-05 04:11:00

问题


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 Image 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


回答1:


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.



来源:https://stackoverflow.com/questions/6688356/add-transparent-and-stretched-image-to-imagelist-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!