unit uDiminishPicsThread;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons,GDIPOBJ,Math,
  System.IOUtils,System.Types, Vcl.ComCtrls,uTestDiminishPics;

type
  TDiminishPicsThread = class(TThread)
  private




    FThreadStarter:TfTestDiminshPics;
    fEx : Exception;
    FPicsArray:TStringDynArray;

    FMaxWidth:integer;
    FMaxHeight:integer;


    procedure QueryError;

    procedure GDIplusDiminishPic(FilePath: string);
    procedure LoadAAImageFromFile(FileName: String; W, H: Integer;
      Pic: TBitmap);
    procedure LoadFromFile(FilePath: string; var twic:twicimage);
    procedure DiminishPic(FilePath: string);

   public



   property PicsInDir:TStringDynArray read FPicsArray write FPicsArray;


   property ThreadStarter:TfTestDiminshPics read FThreadStarter write
   FThreadStarter;


   property MaxWidth:integer read FMaxWidth write FMaxWidth;
   property MaxHeight:integer read FMaxHeight write FMaxHeight;



  protected
    procedure Execute; override;
  end;

implementation



{ TDiminishPicsThread }

procedure TDiminishPicsThread.Execute;
var
  iTick:integer;
  i: Integer;

begin



       FThreadStarter.ProgressBar.Max:=High(PicsInDir);



     iTick:=GetTickCount;

        for i := 0 to High(PicsInDir) do

        begin


          // Anonymously updating progressBar
                       Synchronize(
            procedure
            begin
                FThreadStarter.ProgressBar.Position:=
                  FThreadStarter.ProgressBar.Position+1;
            end
        );


               DiminishPic(PicsInDir[i]);
         //   ( GDIplusDiminishPic(FilesInDir[i])); // <<Works unstable Randomly



                  // Anonymously updating caption
               Synchronize(
          procedure
          begin
            ThreadStarter.Caption:=
            'Npics / total = '+i.ToString+' / ' +length(FPicsArray).ToString+
            ' Time(ms)='+IntToStr(GetTickCount-iTick);
          end
          );



        end;




    fEx := ExceptObject as Exception;
    Synchronize( QueryError );


end;




procedure TDiminishPicsThread.LoadFromFile(FilePath:string; var twic:twicimage);
begin
twic.LoadFromFile(FilePath);
end;


procedure TDiminishPicsThread.QueryError;
begin
Application.OnException(Self,fex);
end;

procedure TDiminishPicsThread.GDIplusDiminishPic(FilePath:string);
var
  thumbnailPicsDir: string;

  bmp, bmpDiminished: TBItmap; twic:TWICImage;

  maxWidth:integer;
  maxHeight:integer;

  currentWidth:integer;
  currentHeight:integer;

  koeff:Extended;
  NewWidth: integer;
  NewHeight: integer;
  CurrentFileName: string;
  FileExtension: string;
  NewFileName: string;

begin

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


//Creating dir for Thumbs if not Existed
thumbnailPicsDir:=ExtractFileDir(Application.ExeName)+'\thumbnailPics';
if not TDirectory.Exists( thumbnailPicsDir) then
TDirectory.CreateDirectory(thumbnailPicsDir);
//----

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

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


        twic:=TWICImage.Create;

        bmp:= Tbitmap.Create;
        bmpDiminished:= Tbitmap.Create;

        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:=ChangeFileext( CurrentFileName, '_thumb.'+FileExtension ); // <<NewExtension

            LoadAAImageFromFile(FilePath,NewWidth,NewHeight,bmpDiminished);
            bmpDiminished.SaveToFile(thumbnailPicsDir+'\'+NewFileName);

        finally
          FreeAndNil(bmp);
          FreeAndNil(bmpDiminished);
          FreeAndNil(twic);
        //  ShowMessage('success');
        end;


end;


//-------------Diminish one pic GDI+---------------
procedure TDiminishPicsThread.LoadAAImageFromFile(FileName: String; W,H: Integer; Pic: TBitmap);
var ImageTemp: TGPImage;
graphicsGDIPlus: TGPGraphics;
begin
if FileExists(FileName) then
  begin
  Pic.Width:=W;
  Pic.Height:=H;
  graphicsGDIPlus:=TGPGraphics.Create (Pic.Canvas.Handle);

  ImageTemp:=TGPImage.Create(FileName ); // Trouble here

  graphicsGDIPlus.DrawImage(ImageTemp , 0,0,W,H);
  ImageTemp.Free;
  graphicsGDIPlus.Free;
  end;
end;



//-------------Diminish one pic Stretch---------------
procedure TDiminishPicsThread.DiminishPic(FilePath:string);
var
  thumbnailPicsDir: string;

  bmp, bmpDiminished: TBItmap; twic:TWICImage;

  maxWidth:integer;
  maxHeight:integer;

  currentWidth:integer;
  currentHeight:integer;

  koeff:Extended;
  NewWidth: integer;
  NewHeight: integer;
  CurrentFileName: string;
  FileExtension: string;
  NewFileName: string;

  Picture:TPicture;
  ms: TMemoryStream;
  size:int64;


begin

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


//Creating dir for Thumbs if not Existed
thumbnailPicsDir:=ExtractFileDir(Application.ExeName)+'\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:=ChangeFileext( CurrentFileName, '_thumb'+FileExtension ); // <<NewExtension


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

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

        finally


          FreeAndNil(twic);

          FreeAndNil(bmp);
          FreeAndNil(bmpDiminished);



         // FreeAndNil(twic);
        //  ShowMessage('success');
        end;


end;



end.
