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, October 13, 2017 4:04:10 PM(UTC)
Nick Villalobos

Groups: Registered
Posts: 119

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

When most people think of cropping an image, they tend to imagine something along the lines of removing the edges on the outer face of an image or removing a border of the image as if it's a frame. Occasionally, you may come across a scenario where you need to crop/split an image equally into thirds.

Below you will see sample code on how you can use the LEADTOOLS Crop command to crop an image into three equal portions. Here is the documentation link for that command:
CropCommand

Code:

string inputFile = @"C:\Users\Public\Documents\LEADTOOLS Images\cannon.jpg";
string outputFolder = @"C:\temp";
RasterImageFormat format = RasterImageFormat.Jpeg;
string ext = "jpg";

using (RasterCodecs codecs = new RasterCodecs())
using (RasterImage image = codecs.Load(inputFile))
{
    CropCommand crop = new CropCommand();
    int width = image.Width;
    int height = image.Height;
    int thirdWidth = width / 3;

    // Crop the first third of the image
    using (RasterImage temp = image.Clone())
    {
        crop.Rectangle = new LeadRect(0, 0, thirdWidth, height);
        crop.Run(temp);
        codecs.Save(temp, Path.Combine(outputFolder, $"cropthird.{ext}"), format, 0);
    }

    // Crop the second third of the image
    using (RasterImage temp = image.Clone())
    {
        crop.Rectangle = new LeadRect(thirdWidth, 0, thirdWidth, height);
        crop.Run(temp);
        codecs.Save(temp, Path.Combine(outputFolder, $"cropthird2.{ext}"), format, 0);
    }

    // Crop the final third of the image
    using (RasterImage temp = image.Clone())
    {
        crop.Rectangle = new LeadRect(width - thirdWidth, 0, thirdWidth, height);
        crop.Run(temp);
        codecs.Save(temp, Path.Combine(outputFolder, $"cropthird3.{ext}"), format, 0);
    }
}


SEE OUTPUT BELOW:


UserPostedImage

Edited by moderator Thursday, June 14, 2018 9:13:53 AM(UTC)  | Reason: Added link to CropCommand and formatted code

Nick Villalobos
Developer Support Engineer
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.