unit ads_BitmapResize; {ads_BitmapResize
Copyright(c)2001 Advanced Delphi Systems (Richard Maley,12613 Maidens Bower Drive, Potomac, MD 20854 USA, phone 301-840-1554, maley@advdelphisys.com, http://www.advdelphisys.com/)

The code herein can be used or modified by anyone. Please retain references to Dick Maley at Advanced Delphi Systems. If you make improvements to the code please send your improvements to maley@advdelphisys.com so that the entire Delphi community can benefit. All comments are welcome.

}

interface
Uses
  Windows, Graphics;
//BitmapResize,BitmapResizeProportional
Function BitmapResize(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Percent      : Integer): Boolean; Overload;

Function BitmapResize(
  Bitmap       : TBitmap;
  Percent      : Integer): Boolean; Overload;

Function BitmapResize(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Percent      : Integer): Boolean; Overload;

Function BitmapResize(
  BitmapFile   : String;
  Percent      : Integer): Boolean; Overload;

Function BitmapResize(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  NewHeight    : Integer;
  NewWidth     : Integer): Boolean; Overload;

Function BitmapResize(
  BitmapFileIn : String;
  BitmapFileOut: String;
  NewHeight    : Integer;
  NewWidth     : Integer): Boolean; Overload;

Function BitmapResize(
  Bitmap       : TBitmap;
  NewHeight    : Integer;
  NewWidth     : Integer): Boolean; Overload;

Function BitmapResize(
  BitmapFile   : String;
  NewHeight    : Integer;
  NewWidth     : Integer): Boolean; Overload;

Function BitmapResizeProportional(
  Bitmap       : TBitmap;
  Percent      : Integer): Boolean; Overload;

Function BitmapResizeProportional(
  BitmapFile   : String;
  Percent      : Integer): Boolean; Overload;

Function BitmapResizeProportional(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Percent      : Integer): Boolean; Overload;

Function BitmapResizeProportional(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Percent      : Integer): Boolean; Overload;

Function BitmapResizeProportional(
  Bitmap       : TBitmap;
  NewDimension : Integer;
  IsWidth      : Boolean): Boolean; Overload;

Function BitmapResizeProportional(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  NewDimension : Integer;
  IsWidth      : Boolean): Boolean; Overload;

Function BitmapResizeProportional(
  BitmapFileIn : String;
  BitmapFileOut: String;
  NewDimension : Integer;
  IsWidth      : Boolean): Boolean; Overload;

implementation

Uses Types, SysUtils;
(*
Function BitmapResizeProportional(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  NewDimension : Integer;
  IsWidth      : Boolean): Boolean;
Var
  inWidthOld   : Integer;
  inWidthNew   : Integer;
  inHeightOld  : Integer;
  inHeightNew  : Integer;
  Bitmap       : TBitmap;
begin
  Bitmap       := TBitmap.Create;
  Try
    Try
      {Create a new bitmap and set its size}
      inWidthOld  := BitmapSource.Width;
      inHeightOld := BitmapSource.Height;
      If IsWidth Then
      Begin
       inWidthNew  := NewDimension;
       inHeightNew := (inHeightOld * inWidthNew) div inWidthOld;
      End
      Else
      Begin
        inHeightNew := NewDimension;
        inWidthNew  := (inWidthOld * inHeightNew) div inHeightOld;
      End;
      Bitmap.Width  := inWidthNew;
      Bitmap.Height := inHeightNew;
      {Copy the palette}
      Bitmap.Palette:=BitmapSource.Palette;
      {Delete the lines needed to shrink}
      SetStretchBltMode(Bitmap.Canvas.Handle,STRETCH_DELETESCANS);
      {Resize it}
      Bitmap.Canvas.Copyrect(Rect(0,
                                 0,
                                 inWidthNew,
                                 inHeightNew),
                            BitmapSource.Canvas,
                            Rect(0,
                                 0,
                                 BitmapSource.Width,
                                 BitmapSource.Height));
      {Copy the palette}
      Bitmap.Palette:=BitmapSource.Palette;
      {Assign the new smaller bitmap}
      BitmapOut.Assign(Bitmap);
      {Free the bitmap}

      Result := True;
    Except
      Result := False;
      Raise;
    End;
  Finally
    Bitmap.Free;
  End;
end;
*)
Function BitmapResizeProportional(
  BitmapFileIn : String;
  BitmapFileOut: String;
  NewDimension : Integer;
  IsWidth      : Boolean): Boolean; Overload;
Var
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
Begin
  Result       := False;
  If Not FileExists(BitmapFileIn) Then Exit;
  BitmapSource := TBitmap.Create();
  BitmapOut    := TBitmap.Create();
  Try
    BitmapSource.LoadFromFile(BitmapFileIn);
    Result :=
      BitmapResizeProportional(
        BitmapSource , //BitmapSource : TBitmap;
        BitmapOut    , //BitmapOut    : TBitmap;
        NewDimension , //NewDimension : Integer;
        IsWidth      );//IsWidth      : Boolean): Boolean; Overload;
    If Result Then
    Begin
      If FileExists(BitmapFileOut) Then DeleteFile(BitmapFileOut);
      BitmapOut.SaveToFile(BitmapFileOut);
    End;
  Finally
    BitmapSource.Free;
    BitmapOut   .Free;
  End;
End;

Function BitmapResizeProportional(
  Bitmap       : TBitmap;
  NewDimension : Integer;
  IsWidth      : Boolean): Boolean; Overload;
Var
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
Begin
  BitmapSource := TBitmap.Create();
  BitmapOut    := TBitmap.Create();
  Try
    BitmapSource.Assign(Bitmap);
    Result :=
      BitmapResizeProportional(
        BitmapSource , //BitmapSource : TBitmap;
        BitmapOut    , //BitmapOut    : TBitmap;
        NewDimension , //NewDimension : Integer;
        IsWidth      );//IsWidth      : Boolean): Boolean; Overload;
    If Result Then
    Begin
      Bitmap.Assign(BitmapOut);
    End;
  Finally
    BitmapSource.Free;
    BitmapOut   .Free;
  End;
End;

Function BitmapResizeProportional(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Percent      : Integer): Boolean; Overload;
Var
  inWidthOld   : Integer;
  NewDimension : Integer;
  IsWidth      : Boolean;
Begin
  Result       := False;
  If Percent < 1 Then Exit;
  If BitmapSource = nil Then Exit;
  IsWidth      := True;
  inWidthOld   := BitmapSource.Width;
  NewDimension := (Percent * inWidthOld) div 100;
  Result :=
    BitmapResizeProportional(
      BitmapSource , //BitmapSource : TBitmap;
      BitmapOut    , //BitmapOut    : TBitmap;
      NewDimension , //NewDimension : Integer;
      IsWidth      );//IsWidth      : Boolean): Boolean; Overload;
End;

Function BitmapResizeProportional(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Percent      : Integer): Boolean; Overload;
Var
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
Begin
  Result       := False;
  If Not FileExists(BitmapFileIn) Then Exit;
  BitmapSource := TBitmap.Create();
  BitmapOut    := TBitmap.Create();
  Try
    BitmapSource.LoadFromFile(BitmapFileIn);
    Result :=
      BitmapResizeProportional(
        BitmapSource , //BitmapSource : TBitmap;
        BitmapOut    , //BitmapOut    : TBitmap;
        Percent      );//Percent      : Integer): Boolean; Overload;
    If Result Then
    Begin
      If FileExists(BitmapFileOut) Then DeleteFile(BitmapFileOut);
      BitmapOut.SaveToFile(BitmapFileOut);
    End;
  Finally
    BitmapSource.Free;
    BitmapOut   .Free;
  End;
End;

Function BitmapResizeProportional(
  Bitmap       : TBitmap;
  Percent      : Integer): Boolean; Overload;
Var
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
Begin
  BitmapSource := TBitmap.Create();
  BitmapOut    := TBitmap.Create();
  Try
    BitmapSource.Assign(Bitmap);
    Result :=
      BitmapResizeProportional(
        BitmapSource , //BitmapSource : TBitmap;
        BitmapOut    , //BitmapOut    : TBitmap;
        Percent      );//Percent      : Integer): Boolean; Overload;
    If Result Then
    Begin
      Bitmap.Assign(BitmapOut);
    End;
  Finally
    BitmapSource.Free;
    BitmapOut   .Free;
  End;
End;

Function BitmapResize(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  NewHeight    : Integer;
  NewWidth     : Integer): Boolean;
Var
  inWidthOld   : Integer;
  inHeightOld  : Integer;
  Bitmap       : TBitmap;
begin
  Bitmap       := TBitmap.Create;
  Try
    Try
      inWidthOld  := BitmapSource.Width;
      inHeightOld := BitmapSource.Height;
      Bitmap.Width  := NewWidth;
      Bitmap.Height := NewHeight;
      Bitmap.Palette:=BitmapSource.Palette;
      SetStretchBltMode(Bitmap.Canvas.Handle,STRETCH_DELETESCANS);
      Bitmap.Canvas.Copyrect(Rect(0,
                                 0,
                                 NewWidth,
                                 NewHeight),
                            BitmapSource.Canvas,
                            Rect(0,
                                 0,
                                 inWidthOld,
                                 inHeightOld));
      Bitmap.Palette:=BitmapSource.Palette;
      BitmapOut.Assign(Bitmap);
      Result := True;
    Except
      Result := False;
      Raise;
    End;
  Finally
    Bitmap.Free;
  End;
end;

Function BitmapResizeProportional(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  NewDimension : Integer;
  IsWidth      : Boolean): Boolean;
Var
  inWidthOld   : Integer;
  inWidthNew   : Integer;
  inHeightOld  : Integer;
  inHeightNew  : Integer;
begin
  inWidthOld  := BitmapSource.Width;
  inHeightOld := BitmapSource.Height;
  If IsWidth Then
  Begin
   inWidthNew  := NewDimension;
   inHeightNew := (inHeightOld * inWidthNew) div inWidthOld;
  End
  Else
  Begin
    inHeightNew := NewDimension;
    inWidthNew  := (inWidthOld * inHeightNew) div inHeightOld;
  End;
  Result :=
    BitmapResize(
      BitmapSource , //BitmapSource : TBitmap;
      BitmapOut    , //BitmapOut    : TBitmap;
      inHeightNew  , //NewHeight    : Integer;
      inWidthNew   );//NewWidth     : Integer): Boolean;
End;

Function BitmapResize(
  Bitmap       : TBitmap;
  NewHeight    : Integer;
  NewWidth     : Integer): Boolean; Overload;
Var
  BitmapOut : TBitmap;
Begin
  BitmapOut := TBitmap.Create();
  Try
    Result :=
      BitmapResize(
        Bitmap       , //BitmapSource : TBitmap;
        BitmapOut    , //BitmapOut    : TBitmap;
        NewHeight    , //NewHeight    : Integer;
        NewWidth     );//NewWidth     : Integer): Boolean;
    If Result Then
    Begin
      Bitmap.Assign(BitmapOut);
    End;
  Finally
    BitmapOut.Free;
  End;
End;


Function BitmapResize(
  BitmapFileIn : String;
  BitmapFileOut: String;
  NewHeight    : Integer;
  NewWidth     : Integer): Boolean; Overload;
Var
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
Begin
  Result       := False;
  If Not FileExists(BitmapFileIn) Then Exit;
  BitmapSource := TBitmap.Create();
  BitmapOut    := TBitmap.Create();
  Try
    BitmapSource.LoadFromFile(BitmapFileIn);
    Result :=
      BitmapResize(
        BitmapSource , //BitmapSource : TBitmap;
        BitmapOut    , //BitmapOut    : TBitmap;
        NewHeight    , //NewHeight    : Integer;
        NewWidth     );//NewWidth     : Integer): Boolean; Overload;
    If Result Then
    Begin
      If FileExists(BitmapFileOut) Then DeleteFile(BitmapFileOut);
      BitmapOut.SaveToFile(BitmapFileOut);
    End;
  Finally
    BitmapSource.Free;
    BitmapOut   .Free;
  End;
End;

Function BitmapResize(
  BitmapFile   : String;
  NewHeight    : Integer;
  NewWidth     : Integer): Boolean; Overload;
Var
  Bitmap       : TBitmap;
Begin
  Result       := False;
  If Not FileExists(BitmapFile) Then Exit;
  Bitmap       := TBitmap.Create();
  Try
    Bitmap.LoadFromFile(BitmapFile);
    Result :=
      BitmapResize(
        Bitmap       , //Bitmap       : TBitmap;
        NewHeight    , //NewHeight    : Integer;
        NewWidth     );//NewWidth     : Integer): Boolean; Overload;
    If Result Then
    Begin
      If FileExists(BitmapFile) Then DeleteFile(BitmapFile);
      Bitmap.SaveToFile(BitmapFile);
    End;
  Finally
    Bitmap.Free;
  End;
End;

Function BitmapResize(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Percent      : Integer): Boolean; Overload;
Begin
  Result :=
    BitmapResizeProportional(
      BitmapSource , //BitmapSource : TBitmap;
      BitmapOut    , //BitmapOut    : TBitmap;
      Percent      );//Percent      : Integer): Boolean; Overload;
End;

Function BitmapResize(
  Bitmap       : TBitmap;
  Percent      : Integer): Boolean; Overload;
Begin
  Result :=
    BitmapResizeProportional(
      Bitmap       , //Bitmap       : TBitmap;
      Percent      );//Percent      : Integer): Boolean; Overload;
End;

Function BitmapResize(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Percent      : Integer): Boolean; Overload;
Begin
  Result :=
    BitmapResizeProportional(
      BitmapFileIn , //BitmapFileIn : String;
      BitmapFileOut, //BitmapFileOut: String;
      Percent      );//Percent      : Integer): Boolean; Overload;
End;

Function BitmapResize(
  BitmapFile   : String;
  Percent      : Integer): Boolean; Overload;
Begin
  Result :=
    BitmapResizeProportional(
      BitmapFile   , //BitmapFile   : String;
      Percent      );//Percent      : Integer): Boolean; Overload;
End;

Function BitmapResizeProportional(
  BitmapFile   : String;
  Percent      : Integer): Boolean; Overload;
Begin
  Result :=
    BitmapResizeProportional(
      BitmapFile   , //BitmapFileIn : String;
      BitmapFile   , //BitmapFileOut: String;
      Percent      );//Percent      : Integer): Boolean; Overload;
End;

end.
                                                                                                          //