在 PostScript 中使用渐变 | .NET

Contents
[ Hide Show ]

在 PS 文档中添加渐变

本文将探讨如何在 PS 文档中使用渐变。

渐变是指颜色从一种颜色平滑过渡到另一种颜色,用于使绘制的图像更加逼真。 由于渐变是一种绘画,因此在 .NET 中,它被实现为 System.Drawing.Brush 的子类。实际上,.NET 平台有两种这样的画笔:

为了在 PsDocument 中设置绘画或描边,我们必须分别将 System.Drawing.Brush 类的对象(用于绘画)和 System.Drawing.Pen 类的对象(用于描边)分别传递给相应的方法。 Aspose.Page for .NET 库处理 .NET 平台提供的所有 System.Drawing.Brush 子类。这些子类包括 System.Drawing.SolidBrushSystem.Drawing.TextureBrushSystem.Drawing.LinearGradientBrushSystem.Drawing.PathGradientBrushSystem.Drawing.HatchBrushSystem.Drawing.Pen 类由于已封装,因此无法扩展,但它包含 System.Drawing.Brush 属性,因此 Aspose.Page for .NET 库也可以使用一整套画笔来绘制线条、勾勒形状和文本的轮廓。

为了在 Aspose.Page for .NET 库中使用渐变色绘制图形对象,需要创建 System.Drawing.LinearGradientBrushSystem.Drawing.PathGradientBrush,并将其传递给 SetPaint()FillText()FillAndStrokeText() 方法(这些方法接受 System.Drawing.Brush 作为参数)。

为了在 Aspose.Page for .NET 库中使用渐变色勾勒图形对象轮廓,需要创建 System.Drawing.LinearGradientBrushSystem.Drawing.PathGradientBrush,然后使用此画笔创建 System.Drawing.Pen,最后将其传递给 SetStroke()OutlineText()FillAndStrokeText() 方法(这些方法接受 System.Drawing.Pen 作为参数)。

在下面的示例中,我们演示了如何填充形状和文本,并使用渐变勾勒文本轮廓。

在新的 PS 文档中使用渐变绘制图形对象的算法包括以下步骤:

  1. 为生成的 PS 文件创建输出流。
  2. 创建 PsSaveOptions
  3. 使用已创建的输出流和保存选项创建 PsDocument
  4. 根据要填充或勾勒轮廓的对象,创建必要的图形路径或字体。
  5. 根据所需的渐变形式,创建 System.Drawing.LinearGradientBrushSystem.Drawing.PathGradientBrush 对象。
  6. 为该画笔设置必要的变换。
  7. 将渐变画笔设置为 PsDocument 中的当前画笔。
  8. 使用当前画笔填充图形路径或填充文本。如果我们使用其中一种接受 System.Drawing.Brush 作为参数的文本填充方法,则可以忽略上一步。
  9. 关闭页面。
  10. 保存文档。

如果我们需要使用渐变来描边(勾勒轮廓)图形对象,而不是最后四步,则需要执行以下操作:

  1. 创建带有渐变画笔的 System.Drawing.Pen 对象。

  2. 将此画笔设置为 PsDocument 中的当前描边。

  3. 使用当前描边勾勒图形路径或勾勒文本轮廓。如果我们使用其中一种接受 System.Drawing.Pen 作为参数的文本勾勒轮廓方法,则可以忽略上一步。

  4. 关闭页面。

  5. 保存文档。

我们提供了 5 个代码片段,演示了不同渐变的用法。

在此代码片段中,我们用两种颜色创建了水平线性渐变,并使用该渐变填充了矩形、填充了文本,还勾勒出了文本的轮廓。

 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 包。它使用 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//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}

结果如下

添加垂直渐变

在此代码片段中,我们用两种颜色创建了一个对角线性渐变,并用该渐变填充了一个矩形。

 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}

结果如下

添加对角渐变

在此代码片段中,我们创建了两种颜色的径向渐变,并用该渐变填充了一个圆圈。

 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}

结果

添加径向渐变 image1

在此代码片段中,我们创建了一个由 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

请参阅 Java 中 PS 文档中的渐变操作。

您可以从 GitHub 下载示例和数据文件。

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.