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

发布于 2024-11-23 18:54:08 字数 1350 浏览 3 评论 0原文

根据我之前在 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:上一个问题的链接是:在Delphi中将拉伸图像添加到ImageList< /a>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

書生途 2024-11-30 18:54:08

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文