Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active September 15, 2022 11:38
Show Gist options
  • Save aspose-com-gists/542ef8811ecf66dac9f7830226671e37 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/542ef8811ecf66dac9f7830226671e37 to your computer and use it in GitHub Desktop.

Aspose.Imaging .NET API allows to easy crop your images or photos with high quality in your .NET application or Web service.

You can:

  • crop one or few images;
  • crop a multi-page image;
  • crop vector images.

Interested ?

You may go further at : https://products.aspose.com/imaging/net/

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using System;
using System.Collections.Generic;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads";
List<string> rasterFormatsWithoutExporter = new List<string>() { "djvu" };
rasterFormatsWithoutExporter.ForEach(
formatExt =>
{
var inputFile = Path.Combine(templatesFolder, $"template.{formatExt}");
var outputFile = Path.Combine(templatesFolder, $"cropped_{formatExt.ToUpper()}.png");
Console.WriteLine("Cropping " + formatExt);
using (var image = (RasterImage)Image.Load(inputFile))
{
// More about Crop oepration can be found
// at https://apireference.aspose.com/imaging/net/aspose.imaging/rasterimage/methods/crop/index
image.Crop(image.Width / 4, image.Width / 4, image.Height / 4, image.Height / 4);
image.Save(outputFile, new PngOptions());
}
File.Delete(outputFile);
}
);
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using System;
using System.Collections.Generic;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads";
List<string> rasterFormats = new List<string>() { "png", "bmp", "apng", "dicom",
"jpg", "jp2", "j2k", "tga", "webp", "tif", "ico"};
rasterFormats.ForEach(
formatExt =>
{
var inputFile = Path.Combine(templatesFolder, $"template.{formatExt}");
var outputFile = Path.Combine(templatesFolder, $"cropped.{formatExt}");
Console.WriteLine("Cropping " + formatExt);
using (var image = (RasterImage)Image.Load(inputFile))
{
// More about Crop oepration can be found
// at https://apireference.aspose.com/imaging/net/aspose.imaging/rasterimage/methods/crop/index
image.Crop(image.Width / 4, image.Width / 4, image.Height / 4, image.Height / 4);
image.Save(outputFile);
}
File.Delete(outputFile);
}
);
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using System;
using System.Collections.Generic;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads";
List<string> metaFormats = new List<string>() { "emf", "emz", "wmf", "wmz" };
metaFormats.ForEach(
formatExt =>
{
var inputFile = Path.Combine(templatesFolder, $"template.{formatExt}");
var outputFile = Path.Combine(templatesFolder, $"cropped.{formatExt}");
Console.WriteLine("Cropping " + formatExt);
using (var image = (MetaImage)Image.Load(inputFile))
{
// More about Crop oepration can be found
// at https://apireference.aspose.com/imaging/net/aspose.imaging/rasterimage/methods/crop/index
image.Crop(image.Width / 4, image.Width / 4, image.Height / 4, image.Height / 4);
image.Save(outputFile);
}
File.Delete(outputFile);
}
);
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using System;
using System.Collections.Generic;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads";
List<string> multipagedRasterFormatsWithoutExporter = new List<string>() { "djvu" };
multipagedRasterFormatsWithoutExporter.ForEach(
formatExt =>
{
var inputFile = Path.Combine(templatesFolder, $"template.{formatExt}");
var outputFile = Path.Combine(templatesFolder, $"cropped_{formatExt.ToUpper()}.tif");
Console.WriteLine("Cropping " + formatExt);
using (var image = Image.Load(inputFile))
{
var multipage = (IMultipageImage)image;
foreach (var page in multipage.Pages)
{
var rasterPage = (RasterImage)page;
// More about Crop oepration can be found
// at https://apireference.aspose.com/imaging/net/aspose.imaging/rasterimage/methods/crop/index
rasterPage.Crop(page.Width / 4, page.Width / 4, page.Height / 4, page.Height / 4);
}
image.Save(outputFile, new TiffOptions(TiffExpectedFormat.TiffJpegRgb));
}
File.Delete(outputFile);
}
);
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using System;
using System.Collections.Generic;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads";
List<string> formats = new List<string>() { "cdr", "cmx", "otg", "odg" };
formats.ForEach(
formatExt =>
{
var inputFile = Path.Combine(templatesFolder, $"template.{formatExt}");
var outputFile = Path.Combine(templatesFolder, $"cropped.{formatExt}.tiff");
using (var image = (Image)Image.Load(inputFile))
{
image.Save(outputFile, new TiffOptions(TiffExpectedFormat.Default));
}
Console.WriteLine("Cropping " + formatExt);
using (var image = (RasterImage)Image.Load(outputFile))
{
// More about Crop oepration can be found
// at https://apireference.aspose.com/imaging/net/aspose.imaging/rasterimage/methods/crop/index
image.Crop(image.Width / 4, image.Width / 4, image.Height / 4, image.Height / 4);
image.Save();
}
File.Delete(outputFile);
}
);
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using System;
using System.Collections.Generic;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads";
List<string> formats = new List<string>() { "svg", "eps" };
formats.ForEach(
formatExt =>
{
var inputFile = Path.Combine(templatesFolder, $"template.{formatExt}");
var outputFile = Path.Combine(templatesFolder, $"cropped.{formatExt}.png");
using (var image = (Image)Image.Load(inputFile))
{
image.Save(outputFile, new PngOptions());
}
Console.WriteLine("Cropping " + formatExt);
using (var image = (RasterImage)Image.Load(outputFile))
{
// More about Crop oepration can be found
// at https://apireference.aspose.com/imaging/net/aspose.imaging/rasterimage/methods/crop/index
image.Crop(image.Width / 4, image.Width / 4, image.Height / 4, image.Height / 4);
image.Save();
}
File.Delete(outputFile);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment