PostScript でのグラデーション操作 | .NET

PSドキュメントにグラデーションを追加する

この記事では、PSドキュメントでグラデーションを使用する方法について説明します。

グラデーションとは、ある色から別の色への滑らかな変化です。描画した画像をよりリアルに見せるために使用されます。 グラデーションはペイントの一種であるため、.NETではSystem.Drawing.Brushのサブクラスとして実装されるのが一般的です。実際、.NETプラットフォームには2つのブラシがあります。

PsDocument でペイントやストロークを設定するには、ペイントの場合はSystem.Drawing.Brushクラスのオブジェクト、ストロークの場合はSystem.Drawing.Penクラスのオブジェクトをそれぞれのメソッドに渡す必要があります。 Aspose.Page for .NET ライブラリは、.NET プラットフォームで提供されている System.Drawing.Brush のすべてのサブクラスを処理します。これらのサブクラスは、System.Drawing.SolidBrushSystem.Drawing.TextureBrushSystem.Drawing.LinearGradientBrushSystem.Drawing.PathGradientBrush、および System.Drawing.HatchBrush です。System.Drawing.Pen クラスはシールされているため拡張できませんが、System.Drawing.Brush をプロパティとして含んでいるため、Aspose.Page for .NET ライブラリでは、直線の描画や図形やテキストのアウトラインにも使用できるブラシの完全なセットを使用できます。

Aspose.Page for .NET ライブラリでグラフィック オブジェクトをグラデーションで 描画 するには、System.Drawing.LinearGradientBrush または System.Drawing.PathGradientBrush を作成し、それを SetPaint() または、System.Drawing.Brush をパラメーターとして受け取る FillText()FillAndStrokeText() メソッドのいずれかに渡す必要があります。

Aspose.Page for .NET ライブラリでグラフィック オブジェクトをグラデーションで アウトライン するには、System.Drawing.LinearGradientBrush または System.Drawing.PathGradientBrush を作成し、このブラシを使用して System.Drawing.Pen を作成し、それを SetStroke() または、System.Drawing.Pen をパラメーターとして受け取る OutlineText()FillAndStrokeText() メソッドのいずれかに渡す必要があります。

以下の例では、図形とテキストを塗りつぶし、グラデーションを使用してテキストのアウトラインを作成する方法を示します。

新しいPSドキュメントでグラフィックオブジェクトにグラデーションを適用するアルゴリズムは、以下の手順で構成されます。

  1. 結果のPSファイル用の出力ストリームを作成します。
  2. PsSaveOptions を作成します。
  3. 既に作成した出力ストリームと保存オプションを使用して、 PsDocument を作成します。
  4. 塗りつぶしまたはアウトライン化するオブジェクトに応じて、必要なグラフィックパスまたはフォントを作成します。
  5. 希望するグラデーションの形状に応じて、System.Drawing.LinearGradientBrush または System.Drawing.PathGradientBrush オブジェクトを作成します。
  6. このブラシに必要な変換を設定します。
  7. PsDocument でグラデーションブラシを現在のペイントとして設定します。
  8. グラフィックパスを現在のペイントで塗りつぶすか、テキストを塗りつぶします。System.Drawing.Brush をパラメータとして受け入れるテキスト塗りつぶしメソッドのいずれかを使用する場合は、前の手順は無視できます。
  9. ページを閉じます。
  10. ドキュメントを保存します。

最後の4つのポイントの代わりに、グラデーションを使用してグラフィックオブジェクトを ストローク(アウトライン) する必要がある場合は、次のようになります。

  1. グラデーションブラシを使用して System.Drawing.Pen オブジェクトを作成します。

  2. このペンを PsDocument の現在のストロークとして設定します。

  3. 現在のストロークでグラフィックパスのアウトラインを作成するか、テキストのアウトラインを作成します。System.Drawing.Pen をパラメータとして受け入れるテキストのアウトライン作成方法を使用する場合は、前の手順は無視できます。

  4. ページを閉じます。

  5. ドキュメントを保存します。

さまざまなグラデーションの使い方を示す 5 つのコードスニペットを用意しました。

このコードスニペットでは、2 色で水平方向の線形グラデーションを作成し、四角形を塗りつぶし、テキストを塗りつぶし、このグラデーションでテキストのアウトラインを作成します。

 1//Create an output stream for PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "HorizontalGradient_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 = 200;
11    float offsetY = 100;
12    float width = 200;
13    float height = 100;
14
15    //Create a graphics path from the first rectangle
16    GraphicsPath path = new GraphicsPath();
17    path.AddRectangle(new RectangleF(offsetX, offsetY, width, height));
18
19    //Create linear gradient brush with the rectangle as bounds, start and end colors
20    LinearGradientBrush brush = new LinearGradientBrush(new RectangleF(0, 0, width, height), Color.FromArgb(150, 0, 0, 0),
21        Color.FromArgb(50, 40, 128, 70), 0f);
22    //Create a transform for the brush. X and Y scale component must be equal to the width and the height of the rectangle respectively.
23    //Translation components are offsets of the rectangle
24    Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
25    //Set  the transform
26    brush.Transform = brushTransform;
27
28    //Set the paint
29    document.SetPaint(brush);
30
31    //Fill the rectangle
32    document.Fill(path);
33
34    //Fill the text with the gradient
35    Font font = new Font("Arial", 96, FontStyle.Bold);
36    document.FillAndStrokeText("ABC", font, 200, 300, brush, new Pen(new SolidBrush(Color.Black), 2));
37
38		//Set current stroke
39		document.SetStroke(brush, 5);
40    //Outline text with the gradient
41    document.OutlineText("ABC", font, 200, 400);
42
43    //Close current page
44    document.ClosePage();
45
46    //Save the document
47    document.Save();
48}

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

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

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

水平グラデーションを追加

このコードスニペットでは、5色で垂直方向の線形グラデーションを作成し、このグラデーションで四角形を塗りつぶします。

 1//Create an output stream for PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "VerticalGradient_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 = 200;
11    float offsetY = 100;
12    float width = 200;
13    float height = 100;
14
15    //Create graphics path from the first rectangle
16    GraphicsPath path = new GraphicsPath();
17    path.AddRectangle(new RectangleF(offsetX, offsetY, width, height));
18
19    //Create an array of interpolation colors
20    Color[] colors = { Color.Red, Color.Green, Color.Blue, Color.Orange, Color.DarkOliveGreen };
21    float[] positions = { 0.0f, 0.1873f, 0.492f, 0.734f, 1.0f };
22    ColorBlend colorBlend = new ColorBlend();
23    colorBlend.Colors = colors;
24    colorBlend.Positions = positions;
25
26    //Create linear gradient brush with the rectangle as bounds, start and end colors
27    LinearGradientBrush brush = new LinearGradientBrush(new RectangleF(0, 0, width, height), Color.Beige, Color.DodgerBlue, 0f);
28    //Set interpolation colors
29    brush.InterpolationColors = colorBlend;
30    //Create a transform for the brush. X and Y scale component must be equal to width and height of the rectangle correspondingly.
31    //Translation components are offsets of the rectangle
32    Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
33    //Rotate the graphics state to get colors change in vertical direction from up to down
34    brushTransform.Rotate(90);
35    //Set the transform
36    brush.Transform = brushTransform;
37
38    //Set the paint
39    document.SetPaint(brush);
40
41    //Fill the rectangle
42    document.Fill(path);
43
44    //Close current page
45    document.ClosePage();
46
47    //Save the document
48    document.Save();
49}

結果はこちらです

垂直グラデーションを追加

このコードスニペットでは、2色で斜めの線形グラデーションを作成し、このグラデーションで四角形を塗りつぶしています。

 1//Create an output stream for PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "DiagonaGradient_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 = 200;
11    float offsetY = 100;
12    float width = 200;
13    float height = 100;
14
15    //Create a graphics path from the first rectangle
16    GraphicsPath path = new GraphicsPath();
17    path.AddRectangle(new RectangleF(offsetX, offsetY, width, height));
18
19    //Create linear gradient brush with the rectangle as bounds, start and end colors
20    LinearGradientBrush brush = new LinearGradientBrush(new RectangleF(0, 0, width, height), Color.FromArgb(255, 255, 0, 0),
21        Color.FromArgb(255, 0, 0, 255), 0f);
22
23    //Create a transform for brush. X and Y scale component must be equal to width and height of the rectangle correspondingly.
24    //Translation components are offsets of the rectangle        
25    Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
26    //Rotate the gradient, than scale and translate to get visible a color transition in required rectangle
27    brushTransform.Rotate(-45);
28    float hypotenuse = (float)System.Math.Sqrt(200 * 200 + 100 * 100);
29    float ratio = hypotenuse / 200;
30    brushTransform.Scale(-ratio, 1);
31    brushTransform.Translate(100 / brushTransform.Elements[0], 0);
32
33    //Set the transform
34    brush.Transform = brushTransform;
35
36    //Set the paint
37    document.SetPaint(brush);
38
39    //Fill the rectangle
40    document.Fill(path);
41
42    //Close current page
43    document.ClosePage();
44
45    //Save the document
46    document.Save();
47}

結果はこちらです

斜めグラデーションを追加

このコードスニペットでは、2色で放射状グラデーションを作成し、円をこのグラデーションで塗りつぶしています。

 1//Create an output stream for PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "RadialGradient1_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 = 200;
11    float offsetY = 100;
12    float width = 200;
13    float height = 200;
14
15    //Create a graphics path from the rectangle bounds
16    RectangleF bounds = new RectangleF(offsetX, offsetY, width, height);
17    GraphicsPath path = new GraphicsPath();
18    path.AddEllipse(bounds);
19
20    //Create and fill color blend object
21    Color[] colors = { Color.White, Color.White, Color.Blue };
22    float[] positions = { 0.0f, 0.2f, 1.0f };
23    ColorBlend colorBlend = new ColorBlend();
24    colorBlend.Colors = colors;
25    colorBlend.Positions = positions;
26
27    GraphicsPath brushRect = new GraphicsPath();
28    brushRect.AddRectangle(new RectangleF(0, 0, width, height));
29
30    //Create path gradient brush with the rectangle as bounds
31    PathGradientBrush brush = new PathGradientBrush(brushRect);
32    //Set interpolation colors
33    brush.InterpolationColors = colorBlend;
34    //Create a transform for the brush. X and Y scale component must be equal to width and height of the rectangle correspondingly.
35    //Translation components are offsets of the rectangle
36    Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
37    //Set the transform
38    brush.Transform = brushTransform;
39
40    //Set the paint
41    document.SetPaint(brush);
42
43    //Fill the rectangle
44    document.Fill(path);
45
46    //Close current page
47    document.ClosePage();
48
49    //Save the document
50    document.Save();
51}

結果

放射状グラデーション画像1を追加

このコードスニペットでは、6色で放射状グラデーションを作成し、このグラデーションで四角形を塗りつぶしています。

 1//Create an output stream for PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "RadialGradient2_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 = 200;
11    float offsetY = 100;
12    float width = 200;
13    float height = 200;
14
15    //Create a graphics path from the rectangle bounds
16    RectangleF bounds = new RectangleF(offsetX, offsetY, width, height);
17    GraphicsPath path = new GraphicsPath();
18    path.AddRectangle(bounds);
19
20    //Create and fill color blend object
21    Color[] colors = { Color.Green, Color.Blue, Color.Black, Color.Yellow, Color.Beige, Color.Red };
22    float[] positions = { 0.0f, 0.2f, 0.3f, 0.4f, 0.9f, 1.0f };
23    ColorBlend colorBlend = new ColorBlend();
24    colorBlend.Colors = colors;
25    colorBlend.Positions = positions;
26
27    GraphicsPath brushRect = new GraphicsPath();
28    brushRect.AddRectangle(new RectangleF(0, 0, width, height));
29
30    //Create path gradient brush with the rectangle as bounds
31    PathGradientBrush brush = new PathGradientBrush(brushRect);
32    //Set interpolation colors
33    brush.InterpolationColors = colorBlend;
34    //Create a transform for the brush. X and Y scale component must be equal to width and height of the rectangle correspondingly.
35    //Translation components are offsets of the rectangle
36    Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
37    //Set the transform
38    brush.Transform = brushTransform;
39
40    //Set the paint
41    document.SetPaint(brush);
42
43    //Fill the rectangle
44    document.Fill(path);
45
46    //Close current page
47    document.ClosePage();
48
49    //Save the document
50    document.Save();
51}

結果

放射状グラデーション画像2を追加

PSドキュメントでのグラデーションの操作については、 Javaをご覧ください。

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.