裁剪 EPS | C# .NET API 解决方案
Contents
[
Hide
Show
]概述
本文讲解如何使用 C# 裁剪 EPS 文件。内容涵盖以下主题:
C# 裁剪 EPS 文件说明
裁剪图像的操作会改变图像的一个或多个边距:左、右、上、下。换句话说,它会从边缘剪切图像的内容。因此,最终的 EPS 图像大小将始终小于原始大小。由于我们的解决方案不处理内容,而是处理 EPS 文件的头文件,因此文件的体积不会改变。
EPS 图像的边距由 BoundingBox 元数据定义。为了进行裁剪,或者说设置新的边界框,我们需要知道 EPS 图像现有的边界框。否则,我们可能会将新边界框的值赋给现有边界框之外,从而导致错误。 因此,在 C# 中裁剪 EPS 图像的步骤如下:
- 使用包含 EPS 文件的输入流初始化 PsDocument 对象。
- 使用静态方法 ExtractEpsBoundingBox 提取图像的现有边界框。
- 创建生成的 EPS 文件的输出流。
- 创建新的边界框。
- 使用静态方法 CropEps 裁剪带有新边界框的 PsDocument 对象。
在 C# 中裁剪 EPS
在以下 C# 代码片段中,我们获取图像的现有边界框并裁剪 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}
初始图像
裁剪后的图像
在我们的EPS 裁剪网页应用程序上在线评估 EPS 裁剪效果。您可以在几秒钟内裁剪 EPS 文件并下载结果。
您可以从 GitHub下载示例和数据文件。