unit uDiminishPic;

interface

uses
  SysUtils, Classes,uniImage,Math,GDIPOBJ,
  Graphics,System.IOUtils,jpeg,pngimage,ServerModule,ActiveX;

type
  TDiminishPic = class(TDataModule)
  private
    procedure Png2Jpeg(aPicStream: TMemoryStream);



    { Private declarations }
  public
    { Public declarations }


    procedure ProportionalDiminishPicSize(

    FilePath: string;
    var uniimageSource, uniimageDest: TUniImage;
    maxWidth,
    maxHeight: Integer);
    {< just diminish pics proportionally}


    procedure ProportionalDiminishPicSize2(
    FilePath:string;
    maxWidth, maxHeight:Integer;
    var bmpDest: TBitmap);
    {< just diminish pics proportionally using GDI+}

    procedure LoadAAImageFromFile(FileName: String; W, H: Integer; Pic: TBitmap);

  end;

function DiminishPic: TDiminishPic;

implementation

{$R *.dfm}

uses
  UniGUIVars, uniGUIMainModule, MainModule;

function DiminishPic: TDiminishPic;
begin
  Result := TDiminishPic(UniMainModule.GetModuleInstance(TDiminishPic));
end;




procedure TDiminishPic.Png2Jpeg(aPicStream:TMemoryStream);
var  JPG:TJpegImage;  PNG:TPngImage;  BMP:TBitmap;
begin
  JPG:=TJpegImage.Create;
  PNG:=TPngImage.Create;
  BMP:=TBitmap.Create;
  try
    aPicStream.Position:=0;
    PNG.LoadFromStream(aPicStream);
    //PNG
    BMP.Assign(PNG);
    //BMP
    JPG.Assign(BMP);
    aPicStream.Clear;
    aPicStream.Position := 0;
    JPG.SaveToStream(aPicStream);
  finally
    FreeAndNil(JPG);
    FreeAndNil(PNG);
    FreeAndNil(BMP);
  end;
end;



procedure TDiminishPic.LoadAAImageFromFile(FileName: String; W,H: Integer; Pic: TBitmap);
var ImageTemp: TGPImage;
    graphicsGDIPlus: TGPGraphics;

    fs:TFileStream;
    ms:TMemoryStream;

    JPG:TJpegImage;  PNG:TPngImage;  BMP:TBitmap;
    Stream: IStream;

begin

if FileExists(FileName) then
  begin


  Pic.Width:=W;
  Pic.Height:=H;


  //if png then convert to jpg
{
  if ExtractFileExt(FileName)='.png' then

   begin
     ms:=TMemoryStream.Create;

     PNG:=TPngImage.Create;
     BMP:=TBitmap.Create;
     JPG:=TJPEGImage.Create;

     PNG.LoadFromFile(FileName);

     bmp.PixelFormat := pf32Bit;
     bmp.HandleType :=  bmDIB;
     BMP.AlphaFormat:=afDefined;

     BMP.Assign(PNG);
     JPG.Assign(BMP);
     JPG.SaveToStream(ms);

     Stream := TStreamAdapter.Create(ms);
     ImageTemp:=TGPImage.Create(Stream );

     PNG.Free;
     BMP.Free;
     JPG.Free;
   end else
}
  //if not png
  ImageTemp:=TGPImage.Create(FileName );  // Trouble was here

  graphicsGDIPlus:=TGPGraphics.Create (Pic.Canvas.Handle);



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


procedure TDiminishPic.ProportionalDiminishPicSize2
(FilePath:string; maxWidth,maxHeight:Integer; var bmpDest:TBitmap);

var uniimage:TUniImage;
  currentWidth: Integer;
  currentHeight: Integer;
  koeff: Extended;
  NewWidth: Integer;
  NewHeight: Integer;


begin
//


uniimage:=TUniImage.Create(UniMainModule.UniApplication);
uniimage.Picture.LoadFromFile(FilePath);

            currentWidth:=uniimage.Picture.Width;
            currentHeight:=uniimage.Picture.Height;

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



if koeff<>1 then

 begin

  NewWidth:=trunc(currentWidth*koeff);
  NewHeight:=Trunc(currentHeight*koeff);
 // bmp:=TBitmap.Create;

  LoadAAImageFromFile(FilePath,NewWidth,NewHeight,bmpDest);



 end else

 begin

 //
  NewWidth:=currentWidth;
  NewHeight:=currentHeight;
 // bmp:=TBitmap.Create;

  LoadAAImageFromFile(FilePath,NewWidth,NewHeight,bmpDest);


 end;




end;



procedure TDiminishPic.ProportionalDiminishPicSize(
FilePath:string;
var uniimageSource:TUniImage;
var uniimageDest:TUniImage;
maxWidth:Integer;
maxHeight:Integer

);
var
  currentWidth: integer;
  currentHeight: integer;
  koeff: Extended;

begin

// Checks...

if FilePath='' then exit;
if not ( Assigned(uniimageSource) ) or not ( Assigned(uniimageDest) ) then exit;


//Proportional diminishing
uniimageSource.Picture.LoadFromFile(FilePath);


currentWidth:=uniimageSource.Picture.Width;
currentHeight:=uniimageSource.Picture.Height;

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

if koeff<>1 then

 begin


  uniimageDest.Width:=Trunc(currentWidth*koeff);//MaxWidth;
  uniimageDest.Height:=Trunc(currentHeight*koeff);// MaxHeight;


  uniimageDest.Proportional:=true;
  uniimageDest.Stretch:=true;
  uniimageDest.Assign(uniimageSource);







 end else

 begin

  uniimageDest.Width:=uniimageSource.Width;
  uniimageDest.Height:=uniimageSource.Height;

  uniimageDest.Assign(uniimageSource);



 end;


end;



initialization
  RegisterModuleClass(TDiminishPic);

end.
