PostScript でのハッチパターンの操作 | .NET
PSドキュメントにハッチパターンを追加する
ハッチパターンは、通常、小さな2色(通常は白黒)のシンプルな画像で表されるテクスチャタイリングパターンです。これらの小さな画像の主な内容は、様々なハッチです。
ハッチによる描画のために、.NETプラットフォームにはSystem.Drawing.Brushから派生したSystem.Drawing.HatchBrushという別のクラスがあります。System.Drawing.TextureBrushとの違いは、タイリングに使用する画像を定義する名前付きの定義済みスタイルが用意されていることです。.NETプラットフォームは53種類のハッチスタイルを提供しており、これら52種類すべてをPsDocumentの塗りつぶしやストローク(アウトライン)に使用できます。
Aspose.Page for .NET ライブラリでグラフィック オブジェクトをハッチ パターンで ペイント するには、System.Drawing.HatchBrush を SetPaint() に渡すか、System.Drawing.Brush をパラメーターとして受け入れる FillText() または FillAndStrokeText() メソッドのいずれかに渡すだけで十分です。
Aspose.Page for .NET ライブラリでグラフィック オブジェクトをハッチ パターンで アウトライン するには、System.Drawing.HacthBrush を使って新しい System.Drawing.Pen を作成し、それを SetStroke() に渡すか、System.Drawing.Pen をパラメーターとして受け入れる OutlineText() または FillAndStrokeText() メソッドのいずれかに渡す必要があります。
以下の例では、まずハッチ パターンで図形を塗りつぶす方法、次に .NET のさまざまなハッチ スタイル、最後にハッチ パターンでテキストを塗りつぶしてアウトライン化する方法について説明します。
新しい PS ドキュメントでグラフィック オブジェクトをハッチ パターンで ペイント するアルゴリズムは、次の手順で構成されます。
- 結果のPSファイル用の出力ストリームを作成します。
- PsSaveOptions を作成します。
- 既に作成した出力ストリームと保存オプションを使用して、 PsDocument を作成します。
- 塗りつぶしまたはアウトライン化するオブジェクトに応じて、必要なグラフィックパスまたはフォントを作成します。
- 希望するスタイルで System.Drawing.HatchBrush オブジェクトを作成します。
- PsDocument でハッチブラシを現在のペイントとして設定します。
- グラフィックパスを現在のペイントで塗りつぶすか、テキストを塗りつぶします。System.Drawing.Brush をパラメーターとして受け入れるテキスト塗りつぶしメソッドのいずれかを使用する場合は、前の手順は無視できます。
- ページを閉じます。
- ドキュメントを保存します。
最後の4点の代わりにハッチパターンでグラフィックオブジェクトを*ストローク(アウトライン)*する必要がある場合は、以下の手順に従います。
ハッチブラシを使用してSystem.Drawing.Penオブジェクトを作成します。
このペンをPsDocumentで現在のストロークに設定します。
現在のストロークでグラフィックパスのアウトラインを作成するか、テキストのアウトラインを作成します。System.Drawing.Penをパラメータとして受け入れるテキストのアウトライン方法を使用する場合は、前の点は無視されます。
ページを閉じます。
ドキュメントを保存します。
1// Demonstrates all embedded hatch patterns that can be used to paint or outline shapes or text in PS document.
2
3string outputFileName = "AddHatchPattern_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
11int x0 = 20;
12int y0 = 100;
13int squareSide = 32;
14int width = 500;
15int sumX = 0;
16
17//Restore graphics state
18document.WriteGraphicsSave();
19
20//Translate to initial point
21document.Translate(x0, y0);
22
23//Create rectngle path for every pattern square
24GraphicsPath path = new GraphicsPath();
25path.AddRectangle(new RectangleF(0, 0, squareSide, squareSide));
26
27//Create pen for outlining pattern square
28Pen pen = new Pen(Color.Black, 2);
29
30//For every hatch pattern style
31for (HatchStyle hatchStyle = 0; hatchStyle <= (HatchStyle)52; hatchStyle++)
32{
33 //Set paint with current hatch brush style
34 document.SetPaint(new HatchBrush(hatchStyle, Color.Black, Color.White));
35
36 //Calculate displacement in order to don't go beyond the page bounds
37 int x = squareSide;
38 int y = 0;
39 if (sumX >= width)
40 {
41 x = -(sumX - squareSide);
42 y += squareSide;
43 }
44 //Translate current graphics state
45 document.Translate(x, y);
46 //Fill pattern square
47 document.Fill(path);
48 //Set stroke
49 document.SetStroke(pen);
50 //Draw square outline
51 document.Draw(path);
52
53 //Calculate distance from X0
54 if (sumX >= width)
55 {
56 sumX = squareSide;
57 }
58 else
59 sumX += x;
60}
61
62//Restore graphics state
63document.WriteGraphicsRestore();
64
65//Fill text with hatch pattern
66HatchBrush brush = new HatchBrush(HatchStyle.DiagonalCross, Color.Red, Color.Yellow);
67System.Drawing.Font font = new System.Drawing.Font("Arial", 96, FontStyle.Bold);
68document.FillAndStrokeText("ABC", font, 200, 300, brush, pen);
69
70//Outline text with hatch pattern
71brush = new HatchBrush(HatchStyle.Percent50, Color.Blue, Color.White);
72document.OutlineText("ABC", font, 200, 400, new Pen(brush, 5));
73
74//Close current page
75document.ClosePage();
76
77//Save the document
78document.Save();Linux、macOS、その他のWindows以外のオペレーティングシステムでは、 Aspose.Page.Drawing NuGetパッケージをご利用いただけます。このパッケージは、System.Drawingシステムライブラリではなく、Aspose.Drawingバックエンドを使用します。
そのため、System.Drawing名前空間ではなく、Aspose.Page.Drawing名前空間をインポートしてください。上記のコードスニペットでは、System.Drawing.Rectangleの代わりにAspose.Page.Drawing.Rectangleが使用され、System.Drawing.Drawing2D.GraphicsPathの代わりにAspose.Page.Drawing.Drawing2D.GraphicsPathが使用されます。GitHubのコード例には、必要な置換がすべて含まれています。
PS ドキュメント内のハッチ パターンの操作については、 Java を参照してください。
このコードを実行した結果は次のように表示されます。

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