Crop EPS | C# .NET API Solution

Overview

This article explains how to crop EPS using C#. It covers the following topics.

C# Crop EPS Description

Croping the image is an operation that changes one of the or several margins of the image: left, right, top and bottom. In other words, it cuts the content of the image from the edges. Thus, a size of the resulting representation of EPS image will always be less than original. The volume of the file will not be changed as our solution doesnt’t work with the content but works with the header of EPS file.

The margins of EPS image is defined by BoundingBox metadata. In order to crop, or in other words, to set up new bounding box, we should to know existing bounding box of EPS image. Otherwise we can assign the values of new bounding box outside of the existing bounding box that results in error. So the steps for cropping EPS image in C# follows:

  1. Initialize PsDocument object with the input stream containing EPS file.
  2. Extract the existing bounding box of the image using static method ExtractEpsBoundingBox.
  3. Create the output stream for resulting EPS file.
  4. Create new bounding box.
  5. Crop PsDocument object with new bounding box with the static method CropEps.

You can check the quality of Aspose.Page EPS Crop and view the results via free online Crop EPS and then view the resulting EPS file with our EPS Viewer web application.

Crop EPS in C#

In the following C# code snippet we get existing bounding box of the image and crop EPS:

 1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
 2
 3// The path to the documents directory.
 4string dataDir = GetDataDir();
 5
 6//Initialize PsDocument object with EPS file
 7PsDocument doc = new PsDocument(dataDir + "input.eps");
 8
 9//Get initial bounding box of EPS image
10int [] initialBoundingBox = doc.ExtractEpsBoundingBox();
11
12//Create an output stream for resized EPS
13using (Stream outputEpsStream = new System.IO.FileStream(dataDir + "output_crop.eps", System.IO.FileMode.Create, System.IO.FileAccess.Write))
14{
15    //Create new bounding box
16    //Bounding box is represented by 4 numbers: x0, y0, x, y, where x0 - left margin, y0 - top margin, x - (x0 + width), y - (y0 + height)
17    float[] newBoundingBox = new float[] { 260, 300, 480, 432 };
18
19    //Crop EPS image and save to the output stream                    
20    //Croping of image is changing of its bounding box so that new values of bounding box will be within initial bounding box, that is
21    //initialBoundingBox[0] <= newBoundingBox[0] <= initialBoundingBox[2]
22    //initialBoundingBox[1] <= newBoundingBox[1] <= initialBoundingBox[3]
23    //initialBoundingBox[0] <= newBoundingBox[2] <= initialBoundingBox[2]
24    //initialBoundingBox[1] <= newBoundingBox[3] <= initialBoundingBox[3]
25    doc.CropEps(outputEpsStream, newBoundingBox);
26}

See Crop EPS in Java and C++.

Initial EPS Image
Initial image
Cropped EPS Image
Cropped image

Evaluate cropping EPS online on our Crop EPS web application. You can crop EPS file and dowload result in a few seconds.

You can download examples and data files from GitHub.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.