unit uDiminishPicThread;

interface

uses
  Windows,SysUtils, Classes,Vcl.Graphics,System.IOUtils,Math;

type
  TDiminishPicThread = class(TThread)
  private
    { Private declarations }

    FNewWidth:integer;
    FNewHeight:integer;
    FFilePath:string;
    FNewFileName:string;

    procedure DiminishPic(Filepath: string; NewWidth, NewHeight: integer; var NewFileName: string);


  public

   property NewWidth:integer read FNewWidth write FNewWidth;
   property NewHeight:Integer read FNewHeight write FNewHeight;
   property FilePath:string read FFilePath write FFilePath;
   property NewFileName:string read FNewFileName write FNewFileName;

  protected
    procedure Execute; override;
  end;

implementation

{
  Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure TDiminishPicThread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end;

    or

    Synchronize(
      procedure
      begin
        Form1.Caption := 'Updated in thread via an anonymous method'
      end
      )
    );

  where an anonymous method is passed.

  Similarly, the developer can call the Queue method with similar parameters as
  above, instead passing another TThread class as the first parameter, putting
  the calling thread in a queue with the other thread.

}

{ TDiminishPicThread }

procedure TDiminishPicThread.Execute;
begin
  { Place thread code here }


  DiminishPic(
  FFilePath,
  FNewWidth,
  FNewHeight,
  FNewFileName

  );

end;


procedure TDiminishPicThread.DiminishPic(
Filepath: string;
NewWidth, NewHeight: integer;
var NewFileName:string);

var
  thumbnailPicsDir: string;

  bmp, bmpDiminished: TBItmap;
  twic:TWICImage;

  maxWidth:integer;
  maxHeight:integer;

  currentWidth:integer;
  currentHeight:integer;

  koeff:Extended;

  CurrentFileName: string;
  FileExtension: string;


  Picture:TPicture;

  size:int64;


begin

//Checks...
if not TFile.Exists(FilePath) then exit;

thumbnailPicsDir:=FilePath+'\thumbnailPics';

if not TDirectory.Exists( thumbnailPicsDir ) then
TDirectory.CreateDirectory(thumbnailPicsDir);

//----

//Getting FileExtension
FileExtension:=ExtractFileExt(FilePath);

//Setting MaxSize On the Exit...
//      maxWidth:=FMaxWidth;
//      maxHeight:=FMaxHeight;


        twic:=TWICImage.Create;

        bmp:= Tbitmap.Create;
        //bmp.PixelFormat:=pf32bit;
        bmp.AlphaFormat:=afDefined;

        bmpDiminished:= Tbitmap.Create;
        //bmpDiminished.PixelFormat:=pf32bit;
        bmpDiminished.AlphaFormat:=afDefined;


        try

             twic.LoadFromFile(FilePath);

             bmp.Assign(twic);

             currentWidth:=bmp.Width; currentHeight:=bmp.Height;

            //Calculating koeff
            if (currentWidth>maxWidth) or (currentHeight>maxHeight) then
            koeff:=Min(maxWidth/currentWidth,maxHeight/currentHeight) else
            koeff:=1;

            // New width and height
            NewWidth:=Trunc(koeff*currentWidth);
            NewHeight:=Trunc(koeff*currentHeight);

            CurrentFileName:=ExtractFileName(FilePath);

     //       NewFileName:=CreateUniqueNameAddingGUID(CurrentFileName, 15);
            NewFileName:=ChangeFileext( NewFileName, '_thumb'+FileExtension ); // <<NewExtension


           bmpDiminished.Width:=NewWidth; bmpDiminished.Height:=NewHeight;
           bmpDiminished.Canvas.StretchDraw(Rect(0, 0, NewWidth, NewHeight), bmp);

          // bmpDiminished.SaveToStream(ms); // diminished pic in memory stream


            bmpDiminished.SaveToFile(thumbnailPicsDir+'\'+NewFileName);

        finally


          FreeAndNil(twic);

          FreeAndNil(bmp);
          FreeAndNil(bmpDiminished);




        //  ShowMessage('success');
        end;


end;


end.
