我有多个图像,其中一些是png,其中一些是jpg和gif,我想在列表视图中显示它们作为缩略图TImageList仅支持图标如何将它们转换为能够在TImageList中插入它们。
我正在使用Delphi XE
答案 0 :(得分:9)
要专门回答这个问题,还要考虑简单的大小调整(对于缩略图),一些示例代码:
var
Img: TImage;
BmImg: TBitmap;
Bmp: TBitmap;
BmpMask: TBitmap;
IconInfo: TIconInfo;
Ico: TIcon;
begin
Img := TImage.Create(nil);
Img.Picture.LoadFromFile(...
BmImg := TBitmap.Create;
BmImg.Assign(Img.Picture.Graphic);
Img.Free;
Bmp := TBitmap.Create;
Bmp.SetSize(ImageList1.Width, ImageList1.Height);
SetStretchBltMode(Bmp.Canvas.Handle, HALFTONE);
StretchBlt(Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height,
BmImg.Canvas.Handle, 0, 0, BmImg.Width, BmImg.Height, SRCCOPY);
BmImg.Free;
BmpMask := TBitmap.Create;
BmpMask.Canvas.Brush.Color := clBlack;
BmpMask.SetSize(Bmp.Width, Bmp.Height);
FillChar(IconInfo, SizeOf(IconInfo), 0);
IconInfo.fIcon := True;
IconInfo.hbmMask := BmpMask.Handle;
IconInfo.hbmColor := Bmp.Handle;
Ico := TIcon.Create;
Ico.Handle := CreateIconIndirect(IconInfo);
ImageList1.AddIcon(Ico);
Bmp.Free;
BmpMask.Free;
Ico.Free; // calls DestroyIcon
end;
或者,无需创建图标:
var
Img: TImage;
BmImg: TBitmap;
Bmp: TBitmap;
begin
Img := TImage.Create(nil);
Img.Picture.LoadFromFile(..
BmImg := TBitmap.Create;
BmImg.Assign(Img.Picture.Graphic);
Img.Free;
Bmp := TBitmap.Create;
Bmp.SetSize(ImageList1.Width, ImageList1.Height);
SetStretchBltMode(Bmp.Canvas.Handle, HALFTONE);
StretchBlt(Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height,
BmImg.Canvas.Handle, 0, 0, BmImg.Width, BmImg.Height, SRCCOPY);
BmImg.Free;
ImageList1.AddMasked(Bmp, clNone);
Bmp.Free;
end;