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// 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}

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

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

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

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.