Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Trucos (https://www.clubdelphi.com/foros/forumdisplay.php?f=52)
-   -   Crear Thumbnails (o mini imágenes) (https://www.clubdelphi.com/foros/showthread.php?t=80406)

Neftali [Germán.Estévez] 08-06-2006 17:26:33

Crear Thumbnails (o mini imágenes)
 
Se plantea el problema de cómo generar lo que normalmente llamamos un "thumbnail" de una imagen que tenemos guardada en un fichero. Añadiendo un poco más de código podríamos generar todos los "thumbnail" de las imagenenes que hay en el directorio. Para ello se utiliza un componente TBitmap.

Vamos con el código:

Código Delphi [-]
var
   bmp: TBitmap;
   jpg: TJpegImage;
   scale: Double;
 begin
   // Abrir la imagen
   if opendialog1.execute then
   begin
     jpg := TJpegImage.Create;
     try
       // Cargar la imagen
       jpg.Loadfromfile(opendialog1.filename);
       if jpg.Height > jpg.Width then
         scale := 50 / jpg.Height
       else
         scale := 50 / jpg.Width;
       bmp := TBitmap.Create;
       try
         //Crear el thumbnail
         bmp.Width := Round(jpg.Width * scale);
         bmp.Height := Round(jpg.Height * scale);
         bmp.Canvas.StretchDraw(bmp.Canvas.Cliprect, jpg);
         // Dibujarlo en el control
         Self.Canvas.Draw(100, 10, bmp);
         // Convertirlo y guardarlo en disco.
         jpg.Assign(bmp);
         jpg.SaveToFile(ChangeFileext(opendialog1.filename, '_thumb.JPG'));
       finally
         bmp.free;
       end;
     finally
       jpg.free;
     end;
   end;

Se necesita un form con un control OpenDialog para abrir el fichero de imagen (JPG).
Y la Unit jpeg en el uses.

zurech 14-06-2006 20:17:57

Hola Neftali, si metemos el código dentro de un "UNIT" en lugar de estar en un "FORM", y adaptándolo, para no utilizar los dialogs, me da un error en la siguiente linea.

Código Delphi [-]
bmp.Canvas.StretchDraw(bmp.Canvas.Cliprect, jpg);

Me indica
Código Delphi [-]
Incompatible types: 'TGraphic' and 'TJPEGImage'

y eso aun copiando los mismos uses en un lado y en otro.

Mi código para ser mas exactos es:

Código Delphi [-]
procedure descargarImagenMiniatura(direccionImagen: string; destino: string; ancho: integer; alto: integer);
var
  Stream: TMemoryStream;
  Jpg: TJPEGImage;
  bmp: TBitmap;
begin
  // Creo un par de cosillas
  Stream:= TMemoryStream.Create;
  Jpg:= TJPEGImage.Create;

  // INICIO: Parte de código que descarga una imagen de internet y la deja en un Stream
  try
    if (DownloadToStream(direccionImagen, Stream)) then
      begin
        try
          Stream.Seek(0,soFromBeginning);
          Jpg.LoadFromStream(Stream);
        except end;
      end;
  finally
    Stream.Free;
  end;
  // FIN: Parte de código que descarga una imagen de internet y la deja en un Stream

  // ----------------------------------------
       try
         //Crear el thumbnail
         bmp.Width := Round(jpg.Width * 50);
         bmp.Height := Round(jpg.Height * 50);
         bmp.Canvas.StretchDraw(bmp.Canvas.Cliprect, jpg);
         // Convertirlo y guardarlo en disco.
         jpg.Assign(bmp);
       finally
         bmp.free;
       end;
  // ----------------------------------------

  // Ahora guardo la imagen en el directorio que me han dicho
  Jpg.SaveToFile(destino + '\pepe.jpg');
end;

zurech 14-06-2006 20:18:26

Se me olvidaba, la pregunta es si sabeis cual puede ser la causa


La franja horaria es GMT +2. Ahora son las 13:00:00.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi