Bien ahora que tenemos las imagenes en un bmp, solo nos falta ajustar su tamaño y agregarlo al TListView. Para ajustar las imagenes tenemos 3 "sabores" diferentes
Código Delphi
[-]
procedure Estirar(Imagen: TGraphic; Ancho, Alto: Integer);
var
Bitmap: TBitmap;
begin
Bitmap:= TBitmap.Create;
try
Bitmap.Width:= Ancho;
Bitmap.Height:= Alto;
Bitmap.Canvas.StretchDraw(Bitmap.Canvas.ClipRect,Imagen);
Imagen.Assign(Bitmap);
finally
Bitmap.Free;
end;
end;
procedure Recortar(Imagen: TGraphic; Ancho, Alto: Integer);
var
Bitmap: TBitmap;
begin
Bitmap:= TBitmap.Create;
try
if (Ancho/Imagen.Width) > (Alto/Imagen.Height) then
Alto:= Trunc((Ancho*Imagen.Height)/Imagen.Width)
else
Ancho:= Trunc((Imagen.Width*Alto)/Imagen.Height);
Bitmap.Width:= Ancho;
Bitmap.Height:= Alto;
Bitmap.Canvas.StretchDraw(Bitmap.Canvas.ClipRect,Imagen);
Imagen.Assign(Bitmap);
finally
Bitmap.Free;
end;
end;
procedure Rellenar(Imagen: TGraphic; Ancho, Alto: Integer; Color: TColor);
var
Bitmap: TBitmap;
R: TRect;
begin
Bitmap:= TBitmap.Create;
try
R:= Rect(0, 0, Ancho, Alto);
if (Ancho/Imagen.Width) < (Alto/Imagen.Height) then
R.Bottom:= Trunc((Ancho*Imagen.Height)/Imagen.Width)
else
R.Right:= Trunc((Imagen.Width*Alto)/Imagen.Height);
Bitmap.Canvas.Brush.Color:= Color;
Bitmap.Width:= Ancho;
Bitmap.Height:= Alto;
Bitmap.Canvas.StretchDraw(R,Imagen);
Imagen.Assign(Bitmap);
finally
Bitmap.Free;
end;
end;
Ahora que tenemos la imagen con el tamaño adecuado, solo falta añadirlo al TListview. Podemos usar una funcion como esta:
Código Delphi
[-]
procedure Agregar(Url: string; Caption: string; ListView: TListView);
var
Bitmap: TBitmap;
ImageList: TImageList;
ListItem: TListItem;
begin
with ListView do
begin
ImageList:= TImageList(LargeImages);
if ImageList <> nil then
begin
Bitmap:= TBitmap.Create;
try
if DownloadToBmp(Url, Bitmap) then
begin
ListItem:= Items.Add;
ListItem.Caption := Caption;
Recortar(Bitmap, ImageList.Width, ImageList.Height);
ListItem.ImageIndex:= ImageList.Add(Bitmap,nil);
end;
finally
Bitmap.Free;
end;
end;
end;
end;
Por ultimo te dejo un ejemplito de todo trabajando.
Código Delphi
[-]
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
ListView: TListView;
ImageList: TImageList;
begin
ListView := TListView.Create(Self);
ImageList:= TImageList.Create(Self);
ImageList.Height:= 100;
ImageList.Width:= 100;
ImageList.Masked:= FALSE;
with ListView do
begin
Parent := Self;
Align := alClient;
LargeImages:= ImageList;
for i:= 0 to 8 do
Agregar('http://www.clubdelphi.com/images/clubdelphi.jpg', 'Foto ' + IntToStr(i), ListView);
end;
end;