PostScript での透明部分の操作 | .NET

PS ドキュメントに透明度を追加する

PostScript は、ベクター グラフィック オブジェクトの描画において透明度をサポートしていません。ただし、半透明(半透明)画像は、完全に透明なピクセルと完全に不透明なピクセルのセットとしてレンダリングできます。このような画像は マスク と呼ばれます。

Aspose.Page for .NET ライブラリには、PS ドキュメントに透明画像を追加するメソッドが用意されています。ベクター グラフィック(図形やテキスト)の描画には、「擬似透明」 を使用できます。

「擬似透明」 とは、アルファ値が 255 未満の色を薄くする処理です。これは、アルファ値が 1 である赤、緑、青の成分を特定の割合で混合することで実現されます。

「擬似透明」 では、もちろん、上の透明レイヤーの下にある下の色のレイヤーが見えることはありませんが、下のレイヤーが白の場合に透明であるような錯覚を生み出します。

PS ドキュメントに透明画像を追加する

前述のとおり、透明画像は マスク として PS ドキュメントに追加できます。Aspose.Page for .NET ライブラリには、この目的で AddTransparentImage() メソッドが用意されています。

このメソッドは、画像が完全に不透明か、完全に透明か、あるいは半透明かを認識します。完全に不透明の場合は AddImage() メソッドで不透明画像として追加され、完全に透明の場合はドキュメントに追加されません。半透明画像の場合は、PostScript 画像 マスク として追加されます。

以下の例では、AddImage()AddTransparentImage() を使用して PS ドキュメントに透明画像を追加する場合の違いを示します。白い半透明画像を表示するには、ページの背景色を白以外に設定しています。

この例では、Aspose.Page for .NET ライブラリを使用して新しい PsDocument に任意の画像を追加するには、次の手順を実行します。

  1. 結果の PS ファイルの出力ストリームを作成します。
  2. デフォルトのオプションで PsSaveOptions オブジェクトを作成します。必要に応じて背景色を変更します。
  3. 作成済みの出力ストリームと保存オプションを使用して、1 ページの PsDocument を作成します。
  4. 新しいグラフィックス状態を作成します。
  5. 画像ファイルから Bitmap を作成します。
  6. 画像に必要な変換を作成します。
  7. 画像が不透明であることが確実な場合は、完全に不透明な画像として PsDocument に追加します (AddImage() メソッドを使用)。不透明であることが確実でない場合は、透明な画像として追加します (AddTransparentImage() メソッドを使用)。
  8. 現在のグラフィックス状態から上位レベルのグラフィックス状態に移行します。
  9. ページを閉じます。
  10. ドキュメントを保存します。
 1//Create an output stream for PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "AddTransparentImage_outPS.ps", FileMode.Create))
 3{
 4    //Create save options with A4 size
 5    PsSaveOptions options = new PsSaveOptions();
 6    //Set page's background color to see white image on it's own transparent background
 7    options.BackgroundColor = Color.FromArgb(211, 8, 48);
 8
 9    // Create new 1-paged PS Document
10    PsDocument document = new PsDocument(outPsStream, options, false);
11
12
13    document.WriteGraphicsSave();
14    document.Translate(20, 100);
15
16    //Create a bitmap from translucent image file
17    using (Bitmap image = new Bitmap(dataDir + "mask1.png"))
18    {
19        //Add this image to the document as usual opaque RGB image
20        document.DrawImage(image, new Matrix(1, 0, 0, 1, 100, 0), Color.Empty);
21    }
22
23    //Again create a bitmap from the same image file
24    using (Bitmap image = new Bitmap(dataDir + "mask1.png"))
25    {
26        //Add this image to the document as transparent image
27        document.DrawTransparentImage(image, new Matrix(1, 0, 0, 1, 350, 0), 255);
28    }
29
30    document.WriteGraphicsRestore();
31
32    //Close current page
33    document.ClosePage();
34
35    //Save the document
36    document.Save();
37}

Linux、macOS、その他のWindows以外のオペレーティングシステムでは、 Aspose.Page.Drawing NuGetパッケージをご利用いただけます。このパッケージは、System.Drawingシステムライブラリではなく、Aspose.Drawingバックエンドを使用します。

そのため、System.Drawing名前空間ではなく、Aspose.Page.Drawing名前空間をインポートしてください。上記および以下のコードスニペットでは、System.Drawing.Bitmapの代わりにAspose.Page.Drawing.Bitmapが、System.Drawing.Drawing2D.Matrixの代わりにAspose.Page.Drawing.Drawing2D.Matrixが使用されます。GitHubのコード例には、必要な置換がすべて含まれています。

PSドキュメントでの透明度の設定については、 Java を参照してください。

このコードを実行した結果は次のとおりです。

透明画像の追加

透明なベクターグラフィックオブジェクトの追加

先ほど、Aspose.Page for .NETライブラリは透明な図形とテキストに淡色化アルゴリズムを使用しており、これを**「疑似透明」**と呼んでいることを説明しました。 以下の例では、同じ色で塗りつぶされた2つの図形の違いを示しています。ただし、最初の図形はアルファ要素なし、2番目の図形はアルファ要素ありです。

 1//Create an output stream for PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "ShowPseudoTransparency_outPS.ps", FileMode.Create))
 3{
 4    //Create save options with A4 size
 5    PsSaveOptions options = new PsSaveOptions();
 6
 7    // Create new 1-paged PS Document
 8    PsDocument document = new PsDocument(outPsStream, options, false);
 9
10    float offsetX = 50;
11    float offsetY = 100;
12    float width = 200;
13    float height = 100;
14
15///////////////////////////////// Create a rectangle with opaque gradient fill /////////////////////////////////////////////////////////
16    GraphicsPath path = new GraphicsPath();
17    path.AddRectangle(new RectangleF(50, 100, 200, 100));
18
19    LinearGradientBrush opaqueBrush = new LinearGradientBrush(new RectangleF(0, 0, 200, 100), Color.FromArgb(0, 0, 0),
20        Color.FromArgb(40, 128, 70), 0f);
21    Matrix brushTransform = new Matrix(200, 0, 0, 100, 50, 100);
22    opaqueBrush.Transform = brushTransform;
23    Aspose.Page.EPS.GradientBrush gradientBrush = new GradientBrush(opaqueBrush);
24    gradientBrush.WrapMode = WrapMode.Clamp;
25
26    document.SetPaint(gradientBrush);
27    document.Fill(path);
28/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
29
30    offsetX = 350;
31
32///////////////////////////////// Create a rectangle with translucent gradient fill ///////////////////////////////////////////////////
33    //Create graphics path from the first rectangle
34    path = new GraphicsPath();
35    path.AddRectangle(new RectangleF(offsetX, offsetY, width, height));
36
37    //Create linear gradient brush colors which transparency are not 255, but 150 and 50. So it are translucent.
38    LinearGradientBrush translucentBrush = new LinearGradientBrush(new RectangleF(0, 0, width, height), Color.FromArgb(150, 0, 0, 0),
39        Color.FromArgb(50, 40, 128, 70), 0f);
40    //Create a transform for the brush.
41    brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
42    //Set the transform
43    translucentBrush.Transform = brushTransform;
44    
45    //Set the paint
46    document.SetPaint(translucentBrush);
47    //Fill the rectangle
48    document.Fill(path);
49/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
50
51    //Close current page
52    document.ClosePage();
53
54    //Save the document
55    document.Save();
56}

PS ドキュメントでの透明性の操作については、 Java および C++ を参照してください。

このコードを実行した結果は次のように表示されます。

擬似透明度を表示

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.