본문 바로가기
Application/Delphi Lecture

Thumbnail 이미지 만들기

by 현이빈이 2008. 7. 24.
반응형

function GenerateThumbnails(const InDir, OutDir, Mask: string): LongInt;
var
  Sr: TSearchRec;
  Master: TImage;
  Slave: TBitmap;
const
  MAX_WIDTH = 320; //set maximum width for thumbnail
  MAX_HEIGHT = 240; //set maximum height for thumbnail
begin
  Master := TImage.Create(form1);
  Slave := TBitmap.Create;
  Result := 0;
  try
    if FindFirst(InDir + '\' + Mask,faAnyFile,Sr) = 0 then
    begin
      repeat
        if (Sr.Name <> '.') and (Sr.Name <> '..') then
        begin
          Master.Picture.LoadFromFile(InDir + '\' + Sr.Name);
          if Master.Picture.Width > Master.Picture.Height then
          begin
            Slave.Width := MAX_WIDTH;
            Slave.Height := Round((MAX_WIDTH / Master.Picture.Width) * Master.Picture.Height);
          end
          else
          begin
            Slave.Width := Round((MAX_HEIGHT / Master.Picture.Height) * Master.Picture.Width);
            Slave.Height := MAX_HEIGHT;
          end;

          Slave.Canvas.StretchDraw(Rect(0,0,Slave.Width,Slave.Height),Master.Picture.Graphic);
          Slave.SaveToFile(OutDir + '\t_' + ChangeFileExt(Sr.Name,'.jpg'));
          Result := Result + 1;
        end;
      until FindNext(Sr) <> 0;

      FindClose(Sr);
    end;
  finally
    Master.Destroy;
    Slave.Destroy;
  end;
end;

반응형