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 than 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//Create an input stream for EPS file
 7using (Stream inputEpsStream = new System.IO.FileStream(dataDir + "input.eps", System.IO.FileMode.Open, System.IO.FileAccess.Read))
 8{
 9    //Initialize PsDocument object with input stream
10    PsDocument doc = new PsDocument(inputEpsStream);
11
12    //Get initial bounding box of EPS image
13    int [] initialBoundingBox = doc.ExtractEpsBoundingBox();
14
15    //Create an output stream for resized EPS
16    using (Stream outputEpsStream = new System.IO.FileStream(dataDir + "output_crop.eps", System.IO.FileMode.Create, System.IO.FileAccess.Write))
17    {
18        //Create new bounding box
19        //Bounding box is represented by 4 numbers: x0, y0, x, y, where x0 - left margin, y0 - top margin, x - (x0 + width), y - (y0 + height)
20        float[] newBoundingBox = new float[] { 260, 300, 480, 432 };
21
22        //Crop EPS image and save to the output stream                    
23        //Croping of image is changing of its bounding box so that new values of bounding box will be within initial bounding box, that is
24        //initialBoundingBox[0] <= newBoundingBox[0] <= initialBoundingBox[2]
25        //initialBoundingBox[1] <= newBoundingBox[1] <= initialBoundingBox[3]
26        //initialBoundingBox[0] <= newBoundingBox[2] <= initialBoundingBox[2]
27        //initialBoundingBox[1] <= newBoundingBox[3] <= initialBoundingBox[3]
28        doc.CropEps(outputEpsStream, newBoundingBox);
29    }
30}

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.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.