unit uTestDiminishPics;

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;

type
  TfTestDiminshPics = class(TForm)
    bStretchDraw: TButton;
    OpenDialog1: TOpenDialog;
    bSomeFunc: TBitBtn;
    GDIPlus: TBitBtn;
    bMultiGDIPlus: TBitBtn;
    procedure bStretchDrawClick(Sender: TObject);
    procedure bSomeFuncClick(Sender: TObject);
    procedure GDIPlusClick(Sender: TObject);
    procedure bMultiGDIPlusClick(Sender: TObject);
  private
    procedure ResizeBitmap(imgo, imgd: TBitmap; nw, nh: Integer);
    procedure LoadAAImageFromFile(FileName: String; W, H: Integer;
      Pic: TBitmap);
    procedure GDIplusDiminishPic(FilePath: string);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fTestDiminshPics: TfTestDiminshPics;

implementation

{$R *.dfm}

procedure TfTestDiminshPics.bMultiGDIPlusClick(Sender: TObject);
begin

GDIplusDiminishPic('123');


end;

procedure TfTestDiminshPics.bSomeFuncClick(Sender: TObject);
var
  bmp, bmp2: TBItmap; twic:TWICImage;
begin
  if opendialog1.execute then begin

    twic:=TWICImage.Create;


    bmp:= Tbitmap.Create;
    try

      twic.LoadFromFile(opendialog1.filename);
      bmp.Assign(twic);


      bmp2 := Tbitmap.Create;
      try


       ResizeBitmap(bmp,bmp2,640,480);
       bmp2.SaveToFile(ChangeFileext( opendialog1.filename, '_thumb.BMP' ));

      finally
        bmp2.free;
      end;
    finally
      bmp.free;
      FreeAndNil(twic);
    end;
  end;
end;





procedure TfTestDiminshPics.bStretchDrawClick(Sender: TObject);


var
  bmp, bmp2: TBItmap; twic:TWICImage;
begin
  if opendialog1.execute then begin

    twic:=TWICImage.Create;


    bmp:= Tbitmap.Create;
    try

      twic.LoadFromFile(opendialog1.filename);
      bmp.Assign(twic);


      bmp2 := Tbitmap.Create;
      try
        bmp2.Width := 640;
        bmp2.Height:= 480;




        bmp2.Canvas.StretchDraw(Rect(0, 0, 640, 480), bmp);
        Self.Canvas.Draw(100, 10, bmp2);
        bmp2.SaveToFile(ChangeFileext( opendialog1.filename, '_thumb.BMP' ));
      finally
        bmp2.free;
      end;
    finally
      bmp.free;
      FreeAndNil(twic);
    end;
  end;
end;



procedure TfTestDiminshPics.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 );
graphicsGDIPlus.DrawImage(ImageTemp , 0,0,W,H);
ImageTemp.Free;
graphicsGDIPlus.Free;
end;
end;

procedure TfTestDiminshPics.GDIPlusClick(Sender: TObject);
var
  bmp, bmpDiminished: TBItmap; twic:TWICImage;

  maxWidth:integer;
  maxHeight:integer;

  currentWidth:integer;
  currentHeight:integer;

  k:Extended;

begin

  maxWidth:=100;
  maxHeight:=100;

  if opendialog1.execute then begin

    twic:=TWICImage.Create;

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

    try

        twic.LoadFromFile(opendialog1.filename);
        bmp.Assign(twic);

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

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

        currentWidth:=Trunc(k*currentWidth); currentHeight:=Trunc(k*currentHeight);

      //  ShowMessage('Width '+bmp.Width.ToString() +' Height'+bmp.Height.ToString());

        LoadAAImageFromFile(OpenDialog1.FileName,currentWidth,currentHeight,bmpDiminished);
        bmpDiminished.SaveToFile(ChangeFileext( opendialog1.filename, '_thumb.BMP' ));


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


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

  bmp, bmpDiminished: TBItmap; twic:TWICImage;

  maxWidth:integer;
  maxHeight:integer;

  currentWidth:integer;
  currentHeight:integer;

  k:Extended;

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);



      maxWidth:=100;
      maxHeight:=100;



        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
            k:=Min(maxWidth/currentWidth,maxHeight/currentHeight) else
            k:=1;

            currentWidth:=Trunc(k*currentWidth); currentHeight:=Trunc(k*currentHeight);

          //  ShowMessage('Width '+bmp.Width.ToString() +' Height'+bmp.Height.ToString());

            LoadAAImageFromFile(FilePath,currentWidth,currentHeight,bmpDiminished);
            bmpDiminished.SaveToFile(ChangeFileext( opendialog1.filename, '_thumb.BMP' ));


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




end;


procedure TfTestDiminshPics.ResizeBitmap(imgo, imgd: TBitmap; nw, nh: Integer);
 var
   xini, xfi, yini, yfi, saltx, salty: single;
   x, y, px, py, tpix: integer;
   PixelColor: TColor;
   r, g, b: longint;

   function MyRound(const X: Double): Integer;
   begin
     Result := Trunc(x);
     if Frac(x) >= 0.5 then
       if x >= 0 then Result := Result + 1
       else
         Result := Result - 1;
     // Result := Trunc(X + (-2 * Ord(X < 0) + 1) * 0.5);
  end;

 begin
   // Set target size

  imgd.Width  := nw;
   imgd.Height := nh;

   // Calcs width & height of every area of pixels of the source bitmap

  saltx := imgo.Width / nw;
   salty := imgo.Height / nh;


   yfi := 0;
   for y := 0 to nh - 1 do
   begin
     // Set the initial and final Y coordinate of a pixel area

    yini := yfi;
     yfi  := yini + salty;
     if yfi >= imgo.Height then yfi := imgo.Height - 1;

     xfi := 0;
     for x := 0 to nw - 1 do
     begin
       // Set the inital and final X coordinate of a pixel area

      xini := xfi;
       xfi  := xini + saltx;
       if xfi >= imgo.Width then xfi := imgo.Width - 1;


       // This loop calcs del average result color of a pixel area
      // of the imaginary grid

      r := 0;
       g := 0;
       b := 0;
       tpix := 0;

       for py := MyRound(yini) to MyRound(yfi) do
       begin
         for px := MyRound(xini) to MyRound(xfi) do
         begin
           Inc(tpix);
           PixelColor := ColorToRGB(imgo.Canvas.Pixels[px, py]);
           r := r + GetRValue(PixelColor);
           g := g + GetGValue(PixelColor);
           b := b + GetBValue(PixelColor);
         end;
       end;

       // Draws the result pixel

      imgd.Canvas.Pixels[x, y] :=
         rgb(MyRound(r / tpix),
         MyRound(g / tpix),
         MyRound(b / tpix)
         );
     end;
   end;
 end;


end.
