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//Create an output stream for PostScript document
2using (Stream outPsStream = new FileStream(dataDir + "AddHatchPattern_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 int x0 = 20;
11 int y0 = 100;
12 int squareSide = 32;
13 int width = 500;
14 int sumX = 0;
15
16 //Create a graphics state
17 document.WriteGraphicsSave();
18
19 //Translate the graphics state to initial point
20 document.Translate(x0, y0);
21
22 //Create a rectangle path for every pattern square
23 GraphicsPath path = new GraphicsPath();
24 path.AddRectangle(new RectangleF(0, 0, squareSide, squareSide));
25
26 //Create a pen for outlining pattern square
27 Pen pen = new Pen(Color.Black, 2);
28
29 //For every hatch pattern style
30 for (HatchStyle hatchStyle = 0; hatchStyle <= (HatchStyle)52; hatchStyle++)
31 {
32 //Set the paint with current hatch brush style
33 document.SetPaint(new HatchBrush(hatchStyle, Color.Black, Color.White));
34
35 //Calculate a displacement in order to don't go beyond the page bounds
36 int x = squareSide;
37 int y = 0;
38 if (sumX >= width)
39 {
40 x = -(sumX - squareSide);
41 y += squareSide;
42 }
43 //Translate current graphics state
44 document.Translate(x, y);
45 //Fill the pattern square
46 document.Fill(path);
47 //Set the stroke
48 document.SetStroke(pen);
49 //Draw the square outline
50 document.Draw(path);
51
52 //Calculate a distance from X0
53 if (sumX >= width)
54 {
55 sumX = squareSide;
56 }
57 else
58 sumX += x;
59 }
60
61 //Exit from current graphics state to upper level graphics state
62 document.WriteGraphicsRestore();
63
64 //Fill the text with the hatch pattern
65 HatchBrush brush = new HatchBrush(HatchStyle.DiagonalCross, Color.Red, Color.Yellow);
66 Font font = new Font("Arial", 96, FontStyle.Bold);
67 document.FillAndStrokeText("ABC", font, 200, 300, brush, pen);
68
69 //Outline the text with hatch pattern
70 brush = new HatchBrush(HatchStyle.Percent50, Color.Blue, Color.White);
71 document.OutlineText("ABC", font, 200, 400, new Pen(brush, 5));
72
73
74 //Close current page
75 document.ClosePage();
76
77 //Save the document
78 document.Save();
79}
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 からダウンロードできます。