PostScript でのグラデーション操作 | .NET
PSドキュメントにグラデーションを追加する
この記事では、PSドキュメントでグラデーションを使用する方法について説明します。
グラデーションとは、ある色から別の色への滑らかな変化です。描画した画像をよりリアルに見せるために使用されます。 グラデーションはペイントの一種であるため、.NETではSystem.Drawing.Brushのサブクラスとして実装されるのが一般的です。実際、.NETプラットフォームには2つのブラシがあります。
- System.Drawing.LinearGradientBrush
- System.Drawing.PathGradientBrush
PsDocument でペイントやストロークを設定するには、ペイントの場合はSystem.Drawing.Brushクラスのオブジェクト、ストロークの場合はSystem.Drawing.Penクラスのオブジェクトをそれぞれのメソッドに渡す必要があります。 Aspose.Page for .NET ライブラリは、.NET プラットフォームで提供されている System.Drawing.Brush のすべてのサブクラスを処理します。これらのサブクラスは、System.Drawing.SolidBrush、System.Drawing.TextureBrush、System.Drawing.LinearGradientBrush、System.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ドキュメントでグラフィックオブジェクトにグラデーションを適用するアルゴリズムは、以下の手順で構成されます。
- 結果のPSファイル用の出力ストリームを作成します。
- PsSaveOptions を作成します。
- 既に作成した出力ストリームと保存オプションを使用して、 PsDocument を作成します。
- 塗りつぶしまたはアウトライン化するオブジェクトに応じて、必要なグラフィックパスまたはフォントを作成します。
- 希望するグラデーションの形状に応じて、System.Drawing.LinearGradientBrush または System.Drawing.PathGradientBrush オブジェクトを作成します。
- このブラシに必要な変換を設定します。
- PsDocument でグラデーションブラシを現在のペイントとして設定します。
- グラフィックパスを現在のペイントで塗りつぶすか、テキストを塗りつぶします。System.Drawing.Brush をパラメータとして受け入れるテキスト塗りつぶしメソッドのいずれかを使用する場合は、前の手順は無視できます。
- ページを閉じます。
- ドキュメントを保存します。
最後の4つのポイントの代わりに、グラデーションを使用してグラフィックオブジェクトを ストローク(アウトライン) する必要がある場合は、次のようになります。
グラデーションブラシを使用して System.Drawing.Pen オブジェクトを作成します。
このペンを PsDocument の現在のストロークとして設定します。
現在のストロークでグラフィックパスのアウトラインを作成するか、テキストのアウトラインを作成します。System.Drawing.Pen をパラメータとして受け入れるテキストのアウトライン作成方法を使用する場合は、前の手順は無視できます。
ページを閉じます。
ドキュメントを保存します。
さまざまなグラデーションの使い方を示す 5 つのコードスニペットを用意しました。
このコードスニペットでは、2 色で水平方向の線形グラデーションを作成し、四角形を塗りつぶし、テキストを塗りつぶし、このグラデーションでテキストのアウトラインを作成します。
1// Paint rectangle and text and draw text with horizontal gradient fill in PS document.
2
3string outputFileName = "HorizontalGradient_outPS.ps";
4
5//Create save options with A4 size
6PsSaveOptions options = new PsSaveOptions();
7
8// Create new 1-paged PS Document
9PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
10
11float offsetX = 200;
12float offsetY = 100;
13float width = 200;
14float height = 100;
15
16//Create graphics path from the first rectangle
17GraphicsPath path = new GraphicsPath();
18path.AddRectangle(new RectangleF(offsetX, offsetY, width, height));
19
20//Create linear gradient brush with rectangle as a bounds, start and end colors
21LinearGradientBrush brush = new LinearGradientBrush(new RectangleF(0, 0, width, height), Color.FromArgb(150, 0, 0, 0),
22 Color.FromArgb(50, 40, 128, 70), 0f);
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
25Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
26//Set transform
27brush.Transform = brushTransform;
28
29//Set paint
30document.SetPaint(brush);
31
32//Fill the rectangle
33document.Fill(path);
34
35//Fill text with gradient
36System.Drawing.Font font = new System.Drawing.Font("Arial", 96, FontStyle.Bold);
37document.FillAndStrokeText("ABC", font, 200, 300, brush, new Pen(new SolidBrush(Color.Black), 2));
38
39//Set current stroke
40document.SetStroke(new Pen(brush, 5));
41//Outline text with gradient
42document.OutlineText("ABC", font, 200, 400);
43
44//Close current page
45document.ClosePage();
46
47//Save the document
48document.Save();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// Paint rectangle with vertical gradient fill in PS document.
2
3string outputFileName = "VerticalGradient_outPS.ps";
4
5//Create save options with A4 size
6PsSaveOptions options = new PsSaveOptions();
7
8// Create new 1-paged PS Document
9PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
10
11float offsetX = 200;
12float offsetY = 100;
13float width = 200;
14float height = 100;
15
16//Create graphics path from the first rectangle
17GraphicsPath path = new GraphicsPath();
18path.AddRectangle(new RectangleF(offsetX, offsetY, width, height));
19
20//Create an array of interpolation colors
21Color[] colors = { Color.Red, Color.Green, Color.Blue, Color.Orange, Color.DarkOliveGreen };
22float[] positions = { 0.0f, 0.1873f, 0.492f, 0.734f, 1.0f };
23ColorBlend colorBlend = new ColorBlend();
24colorBlend.Colors = colors;
25colorBlend.Positions = positions;
26
27//Create linear gradient brush with rectangle as a bounds, start and end colors
28LinearGradientBrush brush = new LinearGradientBrush(new RectangleF(0, 0, width, height), Color.Beige, Color.DodgerBlue, 0f);
29//Set interpolation colors
30brush.InterpolationColors = colorBlend;
31//Create a transform for brush. X and Y scale component must be equal to width and height of the rectangle correspondingly.
32//Translation components are offsets of the rectangle
33Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
34//Rotate transform to get colors change in vertical direction from up to down
35brushTransform.Rotate(90);
36//Set transform
37brush.Transform = brushTransform;
38
39//Set paint
40document.SetPaint(brush);
41
42//Fill the rectangle
43document.Fill(path);
44
45//Close current page
46document.ClosePage();
47
48//Save the document
49document.Save();結果はこちらです

このコードスニペットでは、2色で斜めの線形グラデーションを作成し、このグラデーションで四角形を塗りつぶしています。
1// Paint a circle with 2-colors radial gradient fill in PS document.
2
3string outputFileName = "RadialGradient1_outPS.ps";
4
5//Create save options with A4 size
6PsSaveOptions options = new PsSaveOptions();
7
8// Create new 1-paged PS Document
9PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
10
11float offsetX = 200;
12float offsetY = 100;
13float width = 200;
14float height = 200;
15
16//Create graphics path from the rectangle bounds
17RectangleF bounds = new RectangleF(offsetX, offsetY, width, height);
18GraphicsPath path = new GraphicsPath();
19path.AddEllipse(bounds);
20
21//Create and fill color blend object
22Color[] colors = { Color.White, Color.White, Color.Blue };
23float[] positions = { 0.0f, 0.2f, 1.0f };
24ColorBlend colorBlend = new ColorBlend();
25colorBlend.Colors = colors;
26colorBlend.Positions = positions;
27
28GraphicsPath brushRect = new GraphicsPath();
29brushRect.AddRectangle(new RectangleF(0, 0, width, height));
30
31//Create path gradient brush with rectangle as a bounds
32PathGradientBrush brush = new PathGradientBrush(brushRect);
33//Set interpolation colors
34brush.InterpolationColors = colorBlend;
35//Create a transform for brush. X and Y scale component must be equal to width and height of the rectangle correspondingly.
36//Translation components are offsets of the rectangle
37Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
38//Set transform
39brush.Transform = brushTransform;
40
41//Set paint
42document.SetPaint(brush);
43
44//Fill the rectangle
45document.Fill(path);
46
47//Close current page
48document.ClosePage();
49
50//Save the document
51document.Save();
このコードスニペットでは、2色で放射状グラデーションを作成し、円をこのグラデーションで塗りつぶしています。
1// Paint a circle with 6-colors radial gradient fill in PS document.
2
3string outputFileName = "RadialGradient2_outPS.ps";
4
5//Create save options with A4 size
6PsSaveOptions options = new PsSaveOptions();
7
8// Create new 1-paged PS Document
9PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
10
11float offsetX = 200;
12float offsetY = 100;
13float width = 200;
14float height = 200;
15
16//Create graphics path from the rectangle bounds
17RectangleF bounds = new RectangleF(offsetX, offsetY, width, height);
18GraphicsPath path = new GraphicsPath();
19path.AddRectangle(bounds);
20
21//Create and fill color blend object
22Color[] colors = { Color.Green, Color.Blue, Color.Black, Color.Yellow, Color.Beige, Color.Red };
23float[] positions = { 0.0f, 0.2f, 0.3f, 0.4f, 0.9f, 1.0f };
24ColorBlend colorBlend = new ColorBlend();
25colorBlend.Colors = colors;
26colorBlend.Positions = positions;
27
28GraphicsPath brushRect = new GraphicsPath();
29brushRect.AddRectangle(new RectangleF(0, 0, width, height));
30
31//Create path gradient brush with rectangle as a bounds
32PathGradientBrush brush = new PathGradientBrush(brushRect);
33//Set interpolation colors
34brush.InterpolationColors = colorBlend;
35//Create a transform for brush. X and Y scale component must be equal to width and height of the rectangle correspondingly.
36//Translation components are offsets of the rectangle
37Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
38//Set transform
39brush.Transform = brushTransform;
40
41//Set paint
42document.SetPaint(brush);
43
44//Fill the rectangle
45document.Fill(path);
46
47//Close current page
48document.ClosePage();
49
50//Save the document
51document.Save();結果

このコードスニペットでは、6色で放射状グラデーションを作成し、このグラデーションで四角形を塗りつぶしています。
1// Paint a circle with 2-colors radial gradient fill in PS document.
2
3string outputFileName = "RadialGradient1_outPS.ps";
4
5//Create save options with A4 size
6PsSaveOptions options = new PsSaveOptions();
7
8// Create new 1-paged PS Document
9PsDocument document = new PsDocument(OutputDir + outputFileName, options, false);
10
11float offsetX = 200;
12float offsetY = 100;
13float width = 200;
14float height = 200;
15
16//Create graphics path from the rectangle bounds
17RectangleF bounds = new RectangleF(offsetX, offsetY, width, height);
18GraphicsPath path = new GraphicsPath();
19path.AddEllipse(bounds);
20
21//Create and fill color blend object
22Color[] colors = { Color.White, Color.White, Color.Blue };
23float[] positions = { 0.0f, 0.2f, 1.0f };
24ColorBlend colorBlend = new ColorBlend();
25colorBlend.Colors = colors;
26colorBlend.Positions = positions;
27
28GraphicsPath brushRect = new GraphicsPath();
29brushRect.AddRectangle(new RectangleF(0, 0, width, height));
30
31//Create path gradient brush with rectangle as a bounds
32PathGradientBrush brush = new PathGradientBrush(brushRect);
33//Set interpolation colors
34brush.InterpolationColors = colorBlend;
35//Create a transform for brush. X and Y scale component must be equal to width and height of the rectangle correspondingly.
36//Translation components are offsets of the rectangle
37Matrix brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
38//Set transform
39brush.Transform = brushTransform;
40
41//Set paint
42document.SetPaint(brush);
43
44//Fill the rectangle
45document.Fill(path);
46
47//Close current page
48document.ClosePage();
49
50//Save the document
51document.Save();結果

PSドキュメントでのグラデーションの操作については、 Javaをご覧ください。
サンプルとデータ ファイルは GitHub からダウンロードできます。