BorderRemove example for Delphi

hRgnAll: longint;

//Create the RasterProcess object and UnlockSupport
procedure TForm1.Button1Click(Sender: TObject);
begin
//...
   RasterProc:= CreateComObject(CLASS_LEADRasterProcess) as LEADRasterProcess;
   LEADEventSink1.Connect (RasterProc, _LEADRasterProcessEvents);
   LEADRasterView1.Raster.UnlockSupport (L_SUPPORT_DOCUMENT, 'TestKey');
//...
end;

procedure TForm1.Button2Click(Sender: TObject);
var
   nRet: Integer;
begin
   //Border Remove
   //This example returns a region corresponding to the borders of a bitmap
   //For the example, windows regions are returned in the BorderRemove Event and combined.
   //In practice it would be easier and faster to just return a LEAD region representing the changes
   //The border is removed from the image

   //Enable the doc clean event
   RasterProc.EnableDocCleanEvents:= True;
   //Create a Nil region
   hRgnAll:= CreateRectRgn(0, 0, 0, 0);
   nRet:= RasterProc.BorderRemove (LEADRasterView1.Raster,
                                  BORDER_CALLBACK_REGION + BORDER_USE_VARIANCE,
                                  BORDER_ALL, 20, 9, 3);
   if (nRet = 0) then
   begin
      LEADRasterView1.Raster.FreeRgn ();
       LEADRasterView1.Raster.SetRgnHandle (hRgnAll, 0, 0, L_RGN_SET);
      LEADRasterView1.RgnFrameType:= RGNFRAME_COLOR
   end;
   //delete the Windows Rgn
   DeleteObject (hRgnAll);
end;

procedure TForm1. LEADEventSink1Invoke(Sender: TObject; DispID: Integer;
  const IID: TGUID; LocaleID: Integer; Flags: Word; Params: tagDISPPARAMS;
  varResult, ExcepInfo, ArgErr: Pointer);
var
   hRgn: Longint;
   uBorderToRemove: Longint;
   fBoundingRectLeft: Single;
   fBoundingRectTop: Single;
   fBoundingRectWidth: Single;
   fBoundingRectHeight: Single;
   sBorder : String;
   crOut : String;
   szTemp: array[0..255] of Char;

begin
   case (DispID) of
      LEADRASTERPROCESSEVENTS_BORDERREMOVE:
      begin
         hRgn:= OleVariant(Params.rgvarg^[5]);
         uBorderToRemove:= OleVariant(Params.rgvarg^[4]);
         fBoundingRectLeft:= OleVariant(Params.rgvarg^[3]);
         fBoundingRectTop:= OleVariant(Params.rgvarg^[2]);
         fBoundingRectWidth:= OleVariant(Params.rgvarg^[1]);
         fBoundingRectHeight:= OleVariant(Params.rgvarg^[0]);

          CombineRgn (hRgnAll, hRgnAll, hRgn, RGN_OR);
          Case (uBorderToRemove) of
              BORDER_TOP:
               sBorder:= 'Top';
              BORDER_LEFT:
               sBorder:= 'Left';
              BORDER_RIGHT:
               sBorder:= 'Right';
              BORDER_BOTTOM:
               sBorder:= 'Bottom';
         end;
         crOut := Format('Border - %s Bounds:%d,%d,%d,%d',
                      [sBorder,
                      fBoundingRectLeft,
                      fBoundingRectTop,
                      fBoundingRectWidth,
                      fBoundingRectHeight]);
         OutputDebugString(StrPCopy(szTemp,crOut));
          RasterProc.DocCleanSuccess:= SUCCESS_REMOVE;
      end;
   end;
end;