procedure TMainForm.PreviewListImg;
const
ImgPreviewSize = 64;
var
Rct: TRect;
i, k, ImgPreviewHalfSize: integer;
ImgDraw: TBitmap;
ImgLoad: TPicture;
ImgPreviewList: TImageList;
begin
ImgPreviewHalfSize := Round(ImgPreviewSize/2);
ImgPreviewList := TImageList.Create(Self);
ImgPreviewList.Width:=ImgPreviewSize;
ImgPreviewList.Height:=ImgPreviewSize;
ImgLoad := TPicture.Create;
ImgDraw := TBitmap.Create;
ImgDraw.SetSize(ImgPreviewSize,ImgPreviewSize);
for i := 0 to ShellListView1.Items.Count - 1 do
begin
try
ImgLoad.LoadFromFile(ShellTreeView1.Path + '\'+ShellListView1.Items[i].Caption);
ImgDraw.Canvas.Clear;
ImgDraw.Canvas.Brush.Color:=clNone;
ImgDraw.Canvas.FillRect(0,0,ImgPreviewSize,ImgPreviewSize);
if ((ImgLoad.Width <= ImgPreviewSize) and (ImgLoad.Height <= ImgPreviewSize)) then
begin
ImgDraw.Canvas.Draw(round(ImgPreviewHalfSize-(ImgLoad.Width/2)),
round(ImgPreviewHalfSize-(ImgLoad.Height/2)),
ImgLoad.Bitmap);
ImgPreviewList.Add(ImgDraw,NIL);
ShellListView1.Items[i].ImageIndex:=i;
end
else
begin
Rct.Left := 0;
Rct.Top := 0;
if ImgLoad.Width > ImgLoad.Height then
begin
Rct.Right := ImgPreviewSize;
Rct.Bottom := (ImgPreviewSize * ImgLoad.Height) div ImgLoad.Width;
k := round((ImgPreviewSize - Rct.bottom) /2);
Rct.Top:=k;
Rct.Bottom:=Rct.Bottom+k;
ImgDraw.Canvas.StretchDraw(Rct, ImgLoad.Graphic);
end
else if ImgLoad.Height > ImgLoad.Width then
begin
Rct.Bottom := ImgPreviewSize;
Rct.Right := (ImgPreviewSize * ImgLoad.Width) div ImgLoad.Height;
k := round((ImgPreviewSize - Rct.Right) / 2);
Rct.Left:=k;
Rct.Right:=Rct.Right+k;
ImgDraw.Canvas.StretchDraw(Rct, ImgLoad.Graphic);
end
else
ImgDraw.Canvas.StretchDraw(Rect(0,
0,
ImgPreviewSize,
ImgPreviewSize),
ImgLoad.Graphic);
ImgPreviewList.Add(ImgDraw,NIL);
ShellListView1.Items[i].ImageIndex:=i;
end;
except
// what you want
end;
end;
ShellListView1.LargeImages := ImgPreviewList;
end;