在 PostScript 中使用渐变 | .NET
在 PS 文档中添加渐变
本文将探讨如何在 PS 文档中使用渐变。
渐变是指颜色从一种颜色平滑过渡到另一种颜色,用于使绘制的图像更加逼真。 由于渐变是一种绘画,因此在 .NET 中,它被实现为 System.Drawing.Brush 的子类。实际上,.NET 平台有两种这样的画笔:
- 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() 或 FillText() 或 FillAndStrokeText() 方法(这些方法接受 System.Drawing.Brush 作为参数)。
为了在 Aspose.Page for .NET 库中使用渐变色勾勒图形对象轮廓,需要创建 System.Drawing.LinearGradientBrush 或 System.Drawing.PathGradientBrush,然后使用此画笔创建 System.Drawing.Pen,最后将其传递给 SetStroke() 或 OutlineText() 或 FillAndStrokeText() 方法(这些方法接受 System.Drawing.Pen 作为参数)。
在下面的示例中,我们演示了如何填充形状和文本,并使用渐变勾勒文本轮廓。
在新的 PS 文档中使用渐变绘制图形对象的算法包括以下步骤:
- 为生成的 PS 文件创建输出流。
- 创建 PsSaveOptions。
- 使用已创建的输出流和保存选项创建 PsDocument。
- 根据要填充或勾勒轮廓的对象,创建必要的图形路径或字体。
- 根据所需的渐变形式,创建 System.Drawing.LinearGradientBrush 或 System.Drawing.PathGradientBrush 对象。
- 为该画笔设置必要的变换。
- 将渐变画笔设置为 PsDocument 中的当前画笔。
- 使用当前画笔填充图形路径或填充文本。如果我们使用其中一种接受 System.Drawing.Brush 作为参数的文本填充方法,则可以忽略上一步。
- 关闭页面。
- 保存文档。
如果我们需要使用渐变来描边(勾勒轮廓)图形对象,而不是最后四步,则需要执行以下操作:
创建带有渐变画笔的 System.Drawing.Pen 对象。
将此画笔设置为 PsDocument 中的当前描边。
使用当前描边勾勒图形路径或勾勒文本轮廓。如果我们使用其中一种接受 System.Drawing.Pen 作为参数的文本勾勒轮廓方法,则可以忽略上一步。
关闭页面。
保存文档。
我们提供了 5 个代码片段,演示了不同渐变的用法。
在此代码片段中,我们用两种颜色创建了水平线性渐变,并使用该渐变填充了矩形、填充了文本,还勾勒出了文本的轮廓。
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 包。它使用 Aspose.Drawing 后端,而非 System.Drawing 系统库。
因此,请导入 Aspose.Page.Drawing 命名空间,而不是 System.Drawing 命名空间。在以上及以下代码片段中,将使用 Aspose.Page.Drawing.RectangleF 代替 System.Drawing.RectangleF,使用 Aspose.Page.Drawing.Drawing2D.GraphicsPath 代替 System.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();结果如下

在此代码片段中,我们用两种颜色创建了一个对角线性渐变,并用该渐变填充了一个矩形。
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();
在此代码片段中,我们创建了两种颜色的径向渐变,并用该渐变填充了一个圆圈。
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();结果

请参阅 Java 中 PS 文档中的渐变操作。
您可以从 GitHub 下载示例和数据文件。