C++ 用 API ソリューションによる EPS の切り抜き
概要
この記事では、C++ を使用して EPS をトリミングする 方法について説明します。以下のトピックを取り上げます。
C++ による EPS のトリミング 説明
画像のトリミングとは、画像の左、右、上、下の 1 つまたは複数の余白を変更する操作です。つまり、画像の端からコンテンツを切り取ることになります。
したがって、結果として得られる EPS 画像のサイズは常に元の画像よりも小さくなります。このソリューションはコンテンツではなく EPS ファイルのヘッダーに基づいて動作するため、ファイルサイズは変更されません。
EPS 画像の余白は、BoundingBox メタデータによって定義されます。トリミング、つまり新しいバウンディングボックスを設定するには、EPS 画像の既存のバウンディングボックスを把握している必要があります。そうでない場合、新しいバウンディングボックスの値が既存のバウンディングボックスの外側に設定され、エラーが発生する可能性があります。 したがって、C++ で EPS 画像をトリミングする手順は以下のとおりです。
- EPS ファイルを含む入力ストリームを使用して、 PsDocument オブジェクトを初期化します。
- 静的メソッド ExtractEpsBoundingBox を使用して、画像の既存のバウンディングボックスを抽出します。
- 結果の EPS ファイルの出力ストリームを作成します。
- 新しいバウンディングボックスを作成します。
- 静的メソッド CropEps を使用して、新しいバウンディングボックスで PsDocument オブジェクトをトリミングします。
Aspose.Page EPS Crop の品質をチェックし、無料のオンライン Crop EPS で結果を表示し、その後、結果の EPS ファイルを弊社の EPS Viewer Web アプリケーションで表示できます。
C++ で EPS をトリミング
次の C++ コードスニペットでは、 画像の既存の境界ボックスを取得し、EPS をトリミングしています。
1 // The path to the documents directory.
2 System::String dataDir = RunExamples::GetDataDir_WorkingWithEPS();
3
4 //Create an input stream for EPS file
5 {
6 System::SharedPtr<System::IO::Stream> inputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"input.eps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
7 // Clearing resources under 'using' statement
8 System::Details::DisposeGuard<1> __dispose_guard_1({ inputEpsStream});
9 // ------------------------------------------
10
11 try
12 {
13 //Initialize PsDocument object with input stream
14 System::SharedPtr<PsDocument> doc = System::MakeObject<PsDocument>(inputEpsStream);
15
16 //Get initial bounding box of EPS image
17 System::ArrayPtr<int32_t> initialBoundingBox = doc->ExtractEpsBoundingBox();
18
19 //Create an output stream for resized EPS
20 {
21 System::SharedPtr<System::IO::Stream> outputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"output_crop.eps", System::IO::FileMode::Create, System::IO::FileAccess::Write);
22 // Clearing resources under 'using' statement
23 System::Details::DisposeGuard<1> __dispose_guard_0({ outputEpsStream});
24 // ------------------------------------------
25
26 try
27 {
28 //Create new bounding box
29 //Bounding box is represented by 4 numbers: x0, y0, x, y, where x0 - left margin, y0 - top margin, x - (x0 + width), y - (y0 + height)
30 System::ArrayPtr<float> newBoundingBox = System::MakeArray<float>({260, 300, 480, 432});
31
32 //Crop EPS image and save to the output stream
33 //Croping of image is changing of its bounding box so that new values of bounding box will be within initial bounding box, that is
34 //initialBoundingBox[0] <= newBoundingBox[0] <= initialBoundingBox[2]
35 //initialBoundingBox[1] <= newBoundingBox[1] <= initialBoundingBox[3]
36 //initialBoundingBox[0] <= newBoundingBox[2] <= initialBoundingBox[2]
37 //initialBoundingBox[1] <= newBoundingBox[3] <= initialBoundingBox[3]
38 doc->CropEps(outputEpsStream, newBoundingBox);
39 }
40 catch(...)
41 {
42 __dispose_guard_0.SetCurrentException(std::current_exception());
43 }
44 }
45 }
46 catch(...)
47 {
48 __dispose_guard_1.SetCurrentException(std::current_exception());
49 }
50 }
初期画像
トリミングされた画像
EPS 切り抜き Web アプリケーション で、オンラインで EPS の切り抜きをお試しください。数秒で EPS ファイルの切り抜きと結果のダウンロードが可能です。
サンプルファイルとデータファイルは
GitHub からダウンロードできます。