Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Friday, September 13, 2019 10:15:26 AM(UTC)

Hadi  
Hadi

Groups: Manager, Tech Support, Administrators
Posts: 218

Was thanked: 12 time(s) in 12 post(s)

This code tip will cover how to use the AutoCropRectangleCommand in conjunction with the CropCommand to control which side(s) of an image get cropped.

The AutoCropRectangleCommand is used to get the rectangle to be used to trim the image if AutoCropCommand were used. Use this command in conjunction with CropCommand instead of AutoCropCommand when you want to modify the trim rectangle. For example, you could let the user preview the rectangle, or you could add a margin.

The CropCommand is used to crop a RasterImage

First, an enum is needed to be declared so the side(s) to be cropped can be controlled. The Enum will be a flag so more than 1 side can be used (i.e. only want left and right sides trimmed and leave the top and bottom):

Code:
      [Flags]
      public enum CropSide
      {
         None = 0,
         Left = 1 << 0,
         Top = 1 << 1,
         Right = 1 << 2,
         Bottom = 1 << 3,
         All = ~(-1 << 4)
      }


Then, the method is declared where we utilize the 2 RasterCommands above:

Code:
      static void AutoCropSideCommand(RasterImage image, CropSide autoCropSide)
      {
         AutoCropRectangleCommand autoCropRectangleCommand = new AutoCropRectangleCommand();
         autoCropRectangleCommand.Run(image);

         LeadRect cropRect = autoCropRectangleCommand.Rectangle;

         new CropCommand
         {
            Rectangle = new LeadRect(
               autoCropSide.HasFlag(CropSide.Left) ? cropRect.X : 0,
               autoCropSide.HasFlag(CropSide.Top) ? cropRect.Y : 0,
               autoCropSide.HasFlag(CropSide.Right) ? cropRect.Width : image.Width,
               autoCropSide.HasFlag(CropSide.Bottom) ? cropRect.Height : image.Height)
         }.Run(image);
      }


Finally, you can then call this code via:

Code:
         string fileName = @"sample.png";
         string outputFile = @"sample_autocrop.png";

         using (RasterCodecs codecs = new RasterCodecs())
         using (RasterImage image = codecs.Load(fileName, 1))
         {
            AutoCropSideCommand(image, CropSide.Bottom);
            codecs.Save(image, outputFile, RasterImageFormat.Png, 0);
         }
Hadi Chami
Developer Support Manager
LEAD Technologies, Inc.

LEAD Logo
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.037 seconds.