EPS の切り抜き | C# .NET API ソリューション

概要

この記事では、C# を使って EPS をトリミングする 方法について説明します。以下のトピックを取り上げます。

C# EPS トリミングの説明

画像のトリミングとは、画像の左、右、上、下の余白のいずれか、または複数の余白を変更する操作です。つまり、画像のコンテンツを端から切り取ることになります。そのため、結果として得られる EPS 画像のサイズは常に元のサイズよりも小さくなります。このソリューションは EPS ファイルのコンテンツではなくヘッダーに基づいて処理するため、ファイルサイズは変更されません。

EPS 画像の余白は、BoundingBox メタデータによって定義されます。トリミング、つまり新しいバウンディングボックスを設定するには、EPS 画像の既存のバウンディングボックスを把握する必要があります。そうしないと、新しい境界ボックスの値が既存の境界ボックスの外側に割り当てられ、エラーが発生する可能性があります。 したがって、C# で EPS 画像をトリミングする手順は次のとおりです。

  1. EPSファイルを含む入力ストリームを使用して、 PsDocumentオブジェクトを初期化します。
  2. 静的メソッド ExtractEpsBoundingBoxを使用して、画像の既存の境界ボックスを抽出します。
  3. 結果のEPSファイル用の出力ストリームを作成します。
  4. 新しい境界ボックスを作成します。
  5. 静的メソッド CropEpsを使用して、新しい境界ボックスでPsDocumentオブジェクトをトリミングします。

Aspose.Page EPS Crop の品質を確認し、無料のオンラインツール Crop EPS で結果を表示できます。その後、生成された EPS ファイルを弊社の Web アプリケーション EPS Viewer で表示できます。

C# で EPS をトリミング

次の C# コード スニペットでは、 画像の既存の境界ボックスを取得し、EPS をトリミング しています。

 1// Cropping EPS file.
 2
 3// Initialize PS document with EPS file
 4PsDocument document = new PsDocument(DataDir + "input.eps");
 5
 6string outputFileName = "output_crop.eps";
 7
 8//Get initial bounding box of EPS image
 9int[] initialBoundingBox = document.ExtractEpsBoundingBox();
10
11//Create new bounding box
12//Bounding box is represented by 4 numbers: x0, y0, x, y, where x0 - left margin, y0 - top margin, x - (x0 + width), y - (y0 + height)
13float[] newBoundingBox = new float[] { 260, 300, 480, 432 };
14
15//Crop EPS image and save to the output stream                    
16//Croping of image is changing of its bounding box so that new values of bounding box will be within initial bounding box, that is
17//initialBoundingBox[0] <= newBoundingBox[0] <= initialBoundingBox[2]
18//initialBoundingBox[1] <= newBoundingBox[1] <= initialBoundingBox[3]
19//initialBoundingBox[0] <= newBoundingBox[2] <= initialBoundingBox[2]
20//initialBoundingBox[1] <= newBoundingBox[3] <= initialBoundingBox[3]
21document.CropEps(OutputDir + outputFileName, newBoundingBox);
Example-CropEPS.cs hosted with ❤ by GitHub

Java および C++ の Crop EPS を参照してください。

初期EPS画像
初期画像
切り抜かれたEPS画像
トリミングされた画像

EPS 切り抜き Web アプリケーション で、オンラインで EPS の切り抜きをお試しください。数秒で EPS ファイルの切り抜きと結果のダウンロードが可能です。

サンプルファイルとデータファイルは GitHub からダウンロードできます。