在Delphi中添加透明和拉伸图像到图像列表
根据我之前在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您调用
ImageList.Add
并传递nil
作为遮罩图像。您可以计算与拉伸图像对应的蒙版,也可以调用ImageList.AddMasked
而是让图像列表根据您指定为“透明”颜色的颜色计算遮罩。这就是当您在设计时使用图像列表组件编辑器时会发生的情况。You call
ImageList.Add
and passnil
for the mask image. You can either calculate the mask corresponding to your stretched image, or you can callImageList.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.