Ver Mensaje Individual
  #6  
Antiguo 28-05-2006
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Reputación: 24
seoane Va por buen camino
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 [-]
// Estira la imagen hasta ajustarla a los valores de Ancho y Alto
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;

// Ajusta la imagen, manteniendo la proporcion entre ancho y alto,
// para que uno de los lados se ajuste al valor dado y recorta lo 
// que sobra (Este es mi preferido)
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;

// Ajusta la imagen, manteniendo la proporcion entre ancho y alto,
// para que uno de los lados se ajuste al valor dado y rellena lo que
// falta con el color dado
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;
          // Aqui usar cualquiera de los metodos anteriores, el que mas te guste
          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;
Responder Con Cita