unit ads_BitmapCrop; {ads_BitmapCrop
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.

Routines: BitmapCrop, BitmapToBotHalf, BitmapToLeftHalf, BitmapToRightHalf, BitmapToTopHalf, BitmapTrimBottom, BitmapTrimFromSide, BitmapTrimHeightTo, BitmapTrimLeft, BitmapTrimRight, BitmapTrimTop, BitmapTrimWidthTo

}
interface
Uses
  Windows, Graphics;

Function BitmapToTopHalf(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap
  ): Boolean; Overload;

Function BitmapToTopHalf(
  Bitmap       : TBitmap
  ): Boolean; Overload;

Function BitmapToTopHalf(
  BitmapFileIn : String;
  BitmapFileOut: String
  ): Boolean; Overload;

Function BitmapToTopHalf(
  BitmapFile   : String
  ): Boolean; Overload;

Function BitmapToBotHalf(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap
  ): Boolean; Overload;

Function BitmapToBotHalf(
  Bitmap       : TBitmap
  ): Boolean; Overload;

Function BitmapToBotHalf(
  BitmapFileIn : String;
  BitmapFileOut: String
  ): Boolean; Overload;

Function BitmapToBotHalf(
  BitmapFile   : String
  ): Boolean; Overload;

Function BitmapToLeftHalf(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap
  ): Boolean; Overload;

Function BitmapToLeftHalf(
  Bitmap       : TBitmap
  ): Boolean; Overload;

Function BitmapToLeftHalf(
  BitmapFileIn : String;
  BitmapFileOut: String
  ): Boolean; Overload;

Function BitmapToLeftHalf(
  BitmapFile   : String
  ): Boolean; Overload;

Function BitmapToRightHalf(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap
  ): Boolean; Overload;

Function BitmapToRightHalf(
  Bitmap       : TBitmap
  ): Boolean; Overload;

Function BitmapToRightHalf(
  BitmapFileIn : String;
  BitmapFileOut: String
  ): Boolean; Overload;

Function BitmapToRightHalf(
  BitmapFile   : String
  ): Boolean; Overload;

Function BitmapCrop(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Rect         : TRect
  ): Boolean; Overload;

Function BitmapCrop(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Rect         : TRect
  ): Boolean; Overload;

Function BitmapCrop(
  Bitmap       : TBitmap;
  Top          : Integer;
  Left         : Integer;
  Bottom       : Integer;
  Right        : Integer
  ): Boolean; Overload;

Function BitmapCrop(
  Bitmap       : TBitmap;
  Rect         : TRect
  ): Boolean; Overload;

Function BitmapCrop(
  BitmapFile   : String;
  Rect         : TRect
  ): Boolean; Overload;

Function BitmapCrop(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Top          : Integer;
  Left         : Integer;
  Bottom       : Integer;
  Right        : Integer
  ): Boolean; Overload;

Function BitmapCrop(
  BitmapFile   : String;
  Top          : Integer;
  Left         : Integer;
  Bottom       : Integer;
  Right        : Integer
  ): Boolean; Overload;

Function BitmapCrop(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Top          : Integer;
  Left         : Integer;
  Bottom       : Integer;
  Right        : Integer
  ): Boolean; Overload;

Function BitmapTrimTop(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimTop(
  Bitmap       : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimTop(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimTop(
  BitmapFile   : String;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimLeft(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimLeft(
  Bitmap       : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimLeft(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimLeft(
  BitmapFile   : String;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimBottom(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimBottom(
  Bitmap       : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimBottom(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimBottom(
  BitmapFile   : String;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimRight(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimRight(
  Bitmap       : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimRight(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimRight(
  BitmapFile   : String;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimFromSide(
  Bitmap       : TBitmap;
  Pixels       : Integer;
  IsWidth      : Boolean;
  IsEnd        : Boolean
  ): Boolean; Overload;

Function BitmapTrimFromSide(
  BitmapFile   : String;
  Pixels       : Integer;
  IsWidth      : Boolean;
  IsEnd        : Boolean
  ): Boolean; Overload;

Function BitmapTrimFromSide(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Pixels       : Integer;
  IsWidth      : Boolean;
  IsEnd        : Boolean
  ): Boolean; Overload;

Function BitmapTrimFromSide(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Pixels       : Integer;
  IsWidth      : Boolean;
  IsEnd        : Boolean
  ): Boolean; Overload;

//*****************************************

Function BitmapTrimHeightTo(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimHeightTo(
  Bitmap       : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimHeightTo(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimHeightTo(
  BitmapFile   : String;
  Pixels       : Integer
  ): Boolean; Overload;

//*****************************************

Function BitmapTrimWidthTo(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimWidthTo(
  Bitmap       : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimWidthTo(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Pixels       : Integer
  ): Boolean; Overload;

Function BitmapTrimWidthTo(
  BitmapFile   : String;
  Pixels       : Integer
  ): Boolean; Overload;


implementation

Uses Types, SysUtils;

Function BitmapTrimFromSide(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Pixels       : Integer;
  IsWidth      : Boolean;
  IsEnd        : Boolean
  ): Boolean;
Var
  inWidthOld   : Integer;
  inWidthNew   : Integer;
  inHeightOld  : Integer;
  inHeightNew  : Integer;
  Bitmap       : TBitmap;
  SrcTopAdj    : Integer;
  SrcLeftAdj   : Integer;
  SrcWidthAdj  : Integer;
  SrcHgtAdj    : Integer;
begin
  Result       := False;
  Bitmap       := TBitmap.Create;
  Try
    Try
      {Create a new bitmap and set its size}
      inWidthOld  := BitmapSource.Width;
      inHeightOld := BitmapSource.Height;
      If IsWidth Then
      Begin
        If IsEnd Then
        Begin
          SrcTopAdj   := 0;
          SrcLeftAdj  := 0;
          SrcWidthAdj := inWidthOld-Pixels;
          SrcHgtAdj   := inHeightOld;
          inWidthNew  := inWidthOld-Pixels;
          inHeightNew := inHeightOld;
        End
        Else
        Begin
          SrcTopAdj   := 0;
          SrcLeftAdj  := Pixels;
          SrcWidthAdj := inWidthOld;
          SrcHgtAdj   := inHeightOld;
          inWidthNew  := inWidthOld-Pixels;
          inHeightNew := inHeightOld;
        End;
        If inWidthNew < 1 Then Exit;
      End
      Else
      Begin
        If IsEnd Then
        Begin
          SrcTopAdj   := 0;
          SrcLeftAdj  := 0;
          SrcWidthAdj := inWidthOld;
          SrcHgtAdj   := inHeightOld-Pixels;
          inWidthNew  := inWidthOld;
          inHeightNew := inHeightOld-Pixels;
        End
        Else
        Begin
          SrcTopAdj   := Pixels;
          SrcLeftAdj  := 0;
          SrcWidthAdj := inWidthOld;
          SrcHgtAdj   := inHeightOld;
          inWidthNew  := inWidthOld;
          inHeightNew := inHeightOld-Pixels;
        End;
        If inHeightNew < 1 Then Exit;
      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(SrcLeftAdj,
                                 SrcTopAdj,
                                 SrcWidthAdj,
                                 SrcHgtAdj));
      {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 BitmapTrimFromSide(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Pixels       : Integer;
  IsWidth      : Boolean;
  IsEnd        : 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 :=
      BitmapTrimFromSide(
        BitmapSource , //BitmapSource : TBitmap;
        BitmapOut    , //BitmapOut    : TBitmap;
        Pixels       , //Pixels       : Integer;
        IsWidth      , //IsWidth      : Boolean;
        IsEnd          //IsEnd        : 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 BitmapTrimFromSide(
  Bitmap       : TBitmap;
  Pixels       : Integer;
  IsWidth      : Boolean;
  IsEnd        : Boolean
  ): Boolean; Overload;
Var
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
Begin
  BitmapSource := TBitmap.Create();
  BitmapOut    := TBitmap.Create();
  Try
    BitmapSource.Assign(Bitmap);
    Result :=
      BitmapTrimFromSide(
        BitmapSource , //BitmapSource : TBitmap;
        BitmapOut    , //BitmapOut    : TBitmap;
        Pixels       , //Pixels       : Integer;
        IsWidth      , //IsWidth      : Boolean;
        IsEnd          //IsEnd        : Boolean
                     );//): Boolean; Overload;
    If Result Then
    Begin
      Bitmap.Assign(BitmapOut);
    End;
  Finally
    BitmapSource.Free;
    BitmapOut   .Free;
  End;
End;

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

Function BitmapTrimTop(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := False;
  IsEnd        := False;
  Result :=
    BitmapTrimFromSide(
      BitmapSource , //BitmapSource : TBitmap;
      BitmapOut    , //BitmapOut    : TBitmap;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean;
End;

Function BitmapTrimTop(
  Bitmap       : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := False;
  IsEnd        := False;
  Result :=
    BitmapTrimFromSide(
      Bitmap       , //Bitmap       : TBitmap;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean; Overload;
End;

Function BitmapTrimTop(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := False;
  IsEnd        := False;
  Result :=
    BitmapTrimFromSide(
      BitmapFileIn , //BitmapFileIn : String;
      BitmapFileOut, //BitmapFileOut: String;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean; Overload;
End;

Function BitmapTrimTop(
  BitmapFile   : String;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := False;
  IsEnd        := False;
  Result :=
    BitmapTrimFromSide(
      BitmapFile   , //BitmapFile   : String;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean; Overload;
End;

//******************************************************

Function BitmapTrimLeft(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := True;
  IsEnd        := False;
  Result :=
    BitmapTrimFromSide(
      BitmapSource , //BitmapSource : TBitmap;
      BitmapOut    , //BitmapOut    : TBitmap;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean;
End;

Function BitmapTrimLeft(
  Bitmap       : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := True;
  IsEnd        := False;
  Result :=
    BitmapTrimFromSide(
      Bitmap       , //Bitmap       : TBitmap;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean; Overload;
End;

Function BitmapTrimLeft(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := True;
  IsEnd        := False;
  Result :=
    BitmapTrimFromSide(
      BitmapFileIn , //BitmapFileIn : String;
      BitmapFileOut, //BitmapFileOut: String;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean; Overload;
End;

Function BitmapTrimLeft(
  BitmapFile   : String;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := True;
  IsEnd        := False;
  Result :=
    BitmapTrimFromSide(
      BitmapFile   , //BitmapFile   : String;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean; Overload;
End;

//******************************************************

Function BitmapTrimBottom(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := False;
  IsEnd        := True;
  Result :=
    BitmapTrimFromSide(
      BitmapSource , //BitmapSource : TBitmap;
      BitmapOut    , //BitmapOut    : TBitmap;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean;
End;

Function BitmapTrimBottom(
  Bitmap       : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := False;
  IsEnd        := True;
  Result :=
    BitmapTrimFromSide(
      Bitmap       , //Bitmap       : TBitmap;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean; Overload;
End;

Function BitmapTrimBottom(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := False;
  IsEnd        := True;
  Result :=
    BitmapTrimFromSide(
      BitmapFileIn , //BitmapFileIn : String;
      BitmapFileOut, //BitmapFileOut: String;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean; Overload;
End;

Function BitmapTrimBottom(
  BitmapFile   : String;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := False;
  IsEnd        := True;
  Result :=
    BitmapTrimFromSide(
      BitmapFile   , //BitmapFile   : String;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean; Overload;
End;

//******************************************************

Function BitmapTrimRight(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := True;
  IsEnd        := True;
  Result :=
    BitmapTrimFromSide(
      BitmapSource , //BitmapSource : TBitmap;
      BitmapOut    , //BitmapOut    : TBitmap;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean;
End;

Function BitmapTrimRight(
  Bitmap       : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := True;
  IsEnd        := True;
  Result :=
    BitmapTrimFromSide(
      Bitmap       , //Bitmap       : TBitmap;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean; Overload;
End;

Function BitmapTrimRight(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := True;
  IsEnd        := True;
  Result :=
    BitmapTrimFromSide(
      BitmapFileIn , //BitmapFileIn : String;
      BitmapFileOut, //BitmapFileOut: String;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean; Overload;
End;

Function BitmapTrimRight(
  BitmapFile   : String;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := True;
  IsEnd        := True;
  Result :=
    BitmapTrimFromSide(
      BitmapFile   , //BitmapFile   : String;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean; Overload;
End;

//******************************************************

Function BitmapCrop(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Top          : Integer;
  Left         : Integer;
  Bottom       : Integer;
  Right        : Integer
  ): Boolean; Overload;
Var
  TrimTop      : Integer;
  TrimLeft     : Integer;
  TrimRight    : Integer;
  TrimBot      : Integer;
  SrcHeight    : Integer;
  SrcWidth     : Integer;
  Bitmap       : TBitmap;
  boRetval     : Boolean;
Begin
  Result       := False;
  If BitmapSource = nil Then Exit;
  SrcHeight    := BitmapSource.Height;
  SrcWidth     := BitmapSource.Width;
  If Bottom > SrcHeight Then Bottom := SrcHeight;
  If Right  > SrcWidth  Then Right  := SrcWidth;
  If Top    < 0 Then Top  := 0;
  If Left   < 0 Then Left := 0;
  If (Top=0) And (Left=0) And (Bottom=SrcHeight) And (Right=SrcWidth) Then
  Begin
    BitmapOut.Assign(BitmapSource);
    Result := True;
    Exit;
  End;
  TrimTop      := Top;
  TrimLeft     := Left;
  TrimRight    := SrcWidth-Right;
  TrimBot      := SrcHeight-Bottom;
  Bitmap       := TBitmap.Create();
  Try
    Bitmap.Assign(BitmapSource);
    Result     := True;
    If TrimTop > 0 Then
    Begin
      boRetval :=
        BitmapTrimTop(
          Bitmap       , //Bitmap       : TBitmap;
          TrimTop        //Pixels       : Integer
                       );//): Boolean; Overload;
      If Not boRetval Then
      Begin
        Result := False;
        Exit;
      End;
    End;
    If TrimLeft > 0 Then
    Begin
      boRetval :=
        BitmapTrimLeft(
          Bitmap       , //Bitmap       : TBitmap;
          TrimLeft       //Pixels       : Integer
                       );//): Boolean; Overload;
      If Not boRetval Then
      Begin
        Result := False;
        Exit;
      End;
    End;
    If TrimBot > 0 Then
    Begin
      boRetval :=
        BitmapTrimBottom(
          Bitmap       , //Bitmap       : TBitmap;
          TrimBot       //Pixels       : Integer
                       );//): Boolean; Overload;
      If Not boRetval Then
      Begin
        Result := False;
        Exit;
      End;
    End;
    If TrimRight > 0 Then
    Begin
      boRetval :=
        BitmapTrimRight(
          Bitmap       , //Bitmap       : TBitmap;
          TrimRight      //Pixels       : Integer
                       );//): Boolean; Overload;
      If Not boRetval Then
      Begin
        Result := False;
        Exit;
      End;
    End;
    BitmapOut.Assign(Bitmap);
    Result := True;
  Finally
    Bitmap.Free;
  End;
End;

Function BitmapCrop(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Rect         : TRect
  ): Boolean; Overload;
Var
  Top          : Integer;
  Left         : Integer;
  Bottom       : Integer;
  Right        : Integer;
Begin
  Top          := Rect.Top;
  Left         := Rect.Left;
  Bottom       := Rect.Bottom;
  Right        := Rect.Right;
  Result :=
    BitmapCrop(
      BitmapSource , //BitmapSource : TBitmap;
      BitmapOut    , //BitmapOut    : TBitmap;
      Top          , //Top          : Integer;
      Left         , //Left         : Integer;
      Bottom       , //Bottom       : Integer;
      Right          //Right        : Integer
                   );//): Boolean; Overload;
End;

Function BitmapCrop(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Top          : Integer;
  Left         : Integer;
  Bottom       : Integer;
  Right        : 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 :=
      BitmapCrop(
        BitmapSource , //BitmapSource : TBitmap;
        BitmapOut    , //BitmapOut    : TBitmap;
        Top          , //Top          : Integer;
        Left         , //Left         : Integer;
        Bottom       , //Bottom       : Integer;
        Right          //Right        : 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 BitmapCrop(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Rect         : TRect
  ): Boolean; Overload;
Var
  Top          : Integer;
  Left         : Integer;
  Bottom       : Integer;
  Right        : Integer;
Begin
  Top          := Rect.Top;
  Left         := Rect.Left;
  Bottom       := Rect.Bottom;
  Right        := Rect.Right;
  Result :=
    BitmapCrop(
      BitmapFileIn , //BitmapFileIn : String;
      BitmapFileOut, //BitmapFileOut: String;
      Top          , //Top          : Integer;
      Left         , //Left         : Integer;
      Bottom       , //Bottom       : Integer;
      Right          //Right        : Integer
                   );//): Boolean; Overload;
End;

Function BitmapCrop(
  Bitmap       : TBitmap;
  Top          : Integer;
  Left         : Integer;
  Bottom       : Integer;
  Right        : Integer
  ): Boolean; Overload;
Var
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
Begin
  BitmapSource := TBitmap.Create();
  BitmapOut    := TBitmap.Create();
  Try
    BitmapSource.Assign(Bitmap);
    Result :=
      BitmapCrop(
        BitmapSource , //BitmapSource : TBitmap;
        BitmapOut    , //BitmapOut    : TBitmap;
        Top          , //Top          : Integer;
        Left         , //Left         : Integer;
        Bottom       , //Bottom       : Integer;
        Right          //Right        : Integer
                     );//): Boolean; Overload;
    If Result Then
    Begin
      Bitmap.Assign(BitmapOut);
    End;
  Finally
    BitmapSource.Free;
    BitmapOut   .Free;
  End;
End;

Function BitmapCrop(
  Bitmap       : TBitmap;
  Rect         : TRect
  ): Boolean; Overload;
Var
  Top          : Integer;
  Left         : Integer;
  Bottom       : Integer;
  Right        : Integer;
Begin
  Top          := Rect.Top;
  Left         := Rect.Left;
  Bottom       := Rect.Bottom;
  Right        := Rect.Right;
  Result :=
    BitmapCrop(
      Bitmap       , //Bitmap       : TBitmap;
      Top          , //Top          : Integer;
      Left         , //Left         : Integer;
      Bottom       , //Bottom       : Integer;
      Right          //Right        : Integer
                   );//): Boolean; Overload;
End;

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

Function BitmapCrop(
  BitmapFile   : String;
  Rect         : TRect
  ): Boolean; Overload;
Var
  Top          : Integer;
  Left         : Integer;
  Bottom       : Integer;
  Right        : Integer;
Begin
  Top          := Rect.Top;
  Left         := Rect.Left;
  Bottom       := Rect.Bottom;
  Right        := Rect.Right;
  Result :=
    BitmapCrop(
      BitmapFile   , //BitmapFile   : String;
      Top          , //Top          : Integer;
      Left         , //Left         : Integer;
      Bottom       , //Bottom       : Integer;
      Right          //Right        : Integer
                   );//): Boolean; Overload;
End;

//******************************************************

Function BitmapToTopHalf(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap
  ): Boolean; Overload;
Var
  Pixels : Integer;
Begin
  Pixels := BitmapSource.Height div 2;
  Result :=
    BitmapTrimBottom(
      BitmapSource , //BitmapSource : TBitmap;
      BitmapOut    , //BitmapOut    : TBitmap;
      Pixels         //Pixels       : Integer
                   );//): Boolean; Overload;
End;

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

Function BitmapToTopHalf(
  BitmapFileIn : String;
  BitmapFileOut: String
  ): 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 :=
      BitmapToTopHalf(
        BitmapSource , //BitmapSource : TBitmap;
        BitmapOut      //BitmapOut    : TBitmap;
                     );//): Boolean; Overload;
    If Result Then
    Begin
      If FileExists(BitmapFileOut) Then DeleteFile(BitmapFileOut);
      BitmapOut.SaveToFile(BitmapFileOut);
    End;
  Finally
    BitmapSource.Free;
    BitmapOut   .Free;
  End;
End;

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

//******************************************************

Function BitmapToBotHalf(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap
  ): Boolean; Overload;
Var
  Pixels : Integer;
Begin
  Pixels := BitmapSource.Height div 2;
  Result :=
    BitmapTrimTop(
      BitmapSource , //BitmapSource : TBitmap;
      BitmapOut    , //BitmapOut    : TBitmap;
      Pixels         //Pixels       : Integer
                   );//): Boolean; Overload;
End;

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

Function BitmapToBotHalf(
  BitmapFileIn : String;
  BitmapFileOut: String
  ): 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 :=
      BitmapToBotHalf(
        BitmapSource , //BitmapSource : TBitmap;
        BitmapOut      //BitmapOut    : TBitmap;
                     );//): Boolean; Overload;
    If Result Then
    Begin
      If FileExists(BitmapFileOut) Then DeleteFile(BitmapFileOut);
      BitmapOut.SaveToFile(BitmapFileOut);
    End;
  Finally
    BitmapSource.Free;
    BitmapOut   .Free;
  End;
End;

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

//******************************************************

Function BitmapToLeftHalf(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap
  ): Boolean; Overload;
Var
  Pixels : Integer;
Begin
  Pixels := BitmapSource.Width div 2;
  Result :=
    BitmapTrimRight(
      BitmapSource , //BitmapSource : TBitmap;
      BitmapOut    , //BitmapOut    : TBitmap;
      Pixels         //Pixels       : Integer
                   );//): Boolean; Overload;
End;

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

Function BitmapToLeftHalf(
  BitmapFileIn : String;
  BitmapFileOut: String
  ): 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 :=
      BitmapToLeftHalf(
        BitmapSource , //BitmapSource : TBitmap;
        BitmapOut      //BitmapOut    : TBitmap;
                     );//): Boolean; Overload;
    If Result Then
    Begin
      If FileExists(BitmapFileOut) Then DeleteFile(BitmapFileOut);
      BitmapOut.SaveToFile(BitmapFileOut);
    End;
  Finally
    BitmapSource.Free;
    BitmapOut   .Free;
  End;
End;

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

//******************************************************

Function BitmapToRightHalf(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap
  ): Boolean; Overload;
Var
  Pixels : Integer;
Begin
  Pixels := BitmapSource.Width div 2;
  Result :=
    BitmapTrimLeft(
      BitmapSource , //BitmapSource : TBitmap;
      BitmapOut    , //BitmapOut    : TBitmap;
      Pixels         //Pixels       : Integer
                   );//): Boolean; Overload;
End;

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

Function BitmapToRightHalf(
  BitmapFileIn : String;
  BitmapFileOut: String
  ): 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 :=
      BitmapToRightHalf(
        BitmapSource , //BitmapSource : TBitmap;
        BitmapOut      //BitmapOut    : TBitmap;
                     );//): Boolean; Overload;
    If Result Then
    Begin
      If FileExists(BitmapFileOut) Then DeleteFile(BitmapFileOut);
      BitmapOut.SaveToFile(BitmapFileOut);
    End;
  Finally
    BitmapSource.Free;
    BitmapOut   .Free;
  End;
End;

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

//******************************************************

Function BitmapTrimHeightTo(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := False;
  IsEnd        := True;
  If Pixels > BitmapSource.Height Then Pixels := BitmapSource.Height;
  If Pixels < 1 Then Pixels := 1;
  Pixels := BitmapSource.Height - Pixels;
  Result :=
    BitmapTrimFromSide(
      BitmapSource , //BitmapSource : TBitmap;
      BitmapOut    , //BitmapOut    : TBitmap;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean;
End;

Function BitmapTrimHeightTo(
  Bitmap       : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;
Begin
  Result :=
    BitmapTrimHeightTo(
      Bitmap, //BitmapSource : TBitmap;
      Bitmap, //BitmapOut    : TBitmap;
      Pixels  //Pixels       : Integer
            );//): Boolean; Overload;
End;

Function BitmapTrimHeightTo(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Pixels       : 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 :=
      BitmapTrimHeightTo(
        BitmapSource, //BitmapSource : TBitmap;
        BitmapOut   , //BitmapOut    : TBitmap;
        Pixels        //Pixels       : 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 BitmapTrimHeightTo(
  BitmapFile   : String;
  Pixels       : Integer
  ): Boolean; Overload;
Begin
  Result :=
    BitmapTrimHeightTo(
      BitmapFile, //BitmapFileIn : String;
      BitmapFile, //BitmapFileOut: String;
      Pixels      //Pixels       : Integer
                );//): Boolean; Overload;
End;

//******************************************************

Function BitmapTrimWidthTo(
  BitmapSource : TBitmap;
  BitmapOut    : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;
Var
  IsWidth      : Boolean;
  IsEnd        : Boolean;
Begin
  IsWidth      := True;
  IsEnd        := True;
  If Pixels > BitmapSource.Width Then Pixels := BitmapSource.Width;
  If Pixels < 1 Then Pixels := 1;
  Pixels := BitmapSource.Width - Pixels;
  Result :=
    BitmapTrimFromSide(
      BitmapSource , //BitmapSource : TBitmap;
      BitmapOut    , //BitmapOut    : TBitmap;
      Pixels       , //Pixels       : Integer;
      IsWidth      , //IsWidth      : Boolean;
      IsEnd          //IsEnd        : Boolean
                   );//): Boolean;
End;

Function BitmapTrimWidthTo(
  Bitmap       : TBitmap;
  Pixels       : Integer
  ): Boolean; Overload;
Begin
  Result :=
    BitmapTrimWidthTo(
      Bitmap, //BitmapSource : TBitmap;
      Bitmap, //BitmapOut    : TBitmap;
      Pixels  //Pixels       : Integer
            );//): Boolean; Overload;
End;

Function BitmapTrimWidthTo(
  BitmapFileIn : String;
  BitmapFileOut: String;
  Pixels       : 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 :=
      BitmapTrimWidthTo(
        BitmapSource, //BitmapSource : TBitmap;
        BitmapOut   , //BitmapOut    : TBitmap;
        Pixels        //Pixels       : 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 BitmapTrimWidthTo(
  BitmapFile   : String;
  Pixels       : Integer
  ): Boolean; Overload;
Begin
  Result :=
    BitmapTrimWidthTo(
      BitmapFile, //BitmapFileIn : String;
      BitmapFile, //BitmapFileOut: String;
      Pixels      //Pixels       : Integer
                );//): Boolean; Overload;
End;

end.
                                                                                                          //