LEADTOOLS Support
Imaging
Imaging SDK Examples
HOW TO: Crop Image into Thirds and Save Each Image Separately
#1
Posted
:
Friday, October 13, 2017 4:04:10 PM(UTC)
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:
CropCommandCode:
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:
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.
LEADTOOLS Support
Imaging
Imaging SDK Examples
HOW TO: Crop Image into Thirds and Save Each Image Separately
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.