PostScript での画像操作 | C++
PSドキュメントに画像を追加
Aspose.Page for C++ライブラリは、PSドキュメントに画像を追加するための2つの方法を提供しています。
- 不透明画像用
- 透明画像用
PostScriptは透明度をサポートしていないため、半透明画像は完全に透明なピクセルと完全に不透明なピクセルのセットとしてレンダリングできます。このような画像はマスクと呼ばれます。PSドキュメント内の半透明画像をマスクとして表示し、画像の透明度をより正確に反映させるには、そのような画像に対して何らかのチェックと前処理を施す必要があります。
これらのチェックと前処理には時間がかかります。したがって、画像が完全に不透明であることが確実な場合は、実行時間を節約できる最初の方法を使用することをお勧めします。
2つ目の方法は、画像が完全に不透明か、完全に透明か、あるいは半透明かを認識します。完全に不透明な場合は、最初の方法で不透明画像として追加されます。 完全に透明な場合はドキュメントにはまったく追加されませんが、半透明画像の場合はPostScript画像マスクとして追加されます。
以下の例では、完全に不透明な画像を追加する方法を示します。透明画像の追加方法については、「透明度の操作」の記事で説明します。
この例では、Aspose.Page for C++ライブラリを使用して新しい PsDocumentに画像を追加するために、以下の手順を実行します。
- 結果の PS ファイルの出力ストリームを作成します。
- デフォルトのオプションで PsSaveOptions オブジェクトを作成します。
- 作成済みの出力ストリームと保存オプションを使用して、1 ページの PsDocument を作成します。
- 新しいグラフィックス状態を作成します。
- 画像ファイルから System.Drawing.Bitmap を作成します。
- 画像に必要な変換を作成します。
- 画像を PsDocument オブジェクトに追加します。
- 現在のグラフィックス状態から上位レベルのグラフィックス状態に移行します。
- ページを閉じます。
- ドキュメントを保存します。
1 // The path to the documents directory.
2 System::String dataDir = RunExamples::GetDataDir_WorkingWithImages();
3
4 //Create output stream for PostScript document
5 {
6 System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"AddImage_outPS.ps", System::IO::FileMode::Create);
7 // Clearing resources under 'using' statement
8 System::Details::DisposeGuard<1> __dispose_guard_1({ outPsStream});
9 // ------------------------------------------
10
11 try
12 {
13 //Create save options with A4 size
14 System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
15
16 // Create new 1-paged PS Document
17 System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, false);
18
19
20 document->WriteGraphicsSave();
21 document->Translate(100.0f, 100.0f);
22
23 //Create a Bitmap object from image file
24 {
25 System::SharedPtr<System::Drawing::Bitmap> image = System::MakeObject<System::Drawing::Bitmap>(dataDir + u"TestImage Format24bppRgb.jpg");
26 // Clearing resources under 'using' statement
27 System::Details::DisposeGuard<1> __dispose_guard_0({ image});
28 // ------------------------------------------
29
30 try
31 {
32 //Create image transform
33 System::SharedPtr<System::Drawing::Drawing2D::Matrix> transform = System::MakeObject<System::Drawing::Drawing2D::Matrix>();
34 transform->Translate(35.0f, 300.0f);
35 transform->Scale(3.0f, 3.0f);
36 transform->Rotate(-45.0f);
37
38 //Add image to document
39 document->DrawImage(image, transform, System::Drawing::Color::Empty);
40 }
41 catch(...)
42 {
43 __dispose_guard_0.SetCurrentException(std::current_exception());
44 }
45 }
46
47 document->WriteGraphicsRestore();
48
49 //Close current page
50 document->ClosePage();
51
52 //Save the document
53 document->Save();
54 }
55 catch(...)
56 {
57 __dispose_guard_1.SetCurrentException(std::current_exception());
58 }
59 }
このコードを実行した結果は次のように表示されます。
サンプルとデータ ファイルは GitHub からダウンロードできます。