Working with Gradient in PostScript | .NET

Contents
[ Hide Show ]

Add Gradient in PS Document

In this article, we consider the ways how a gradient can be used in PS documents.

The gradient is a smooth transition of one color to another. It is used for making the drawn pictures more realistic. As gradient is a kind of paint, it is expectedly that in .NET it is implemented as a subclass of System.Drawing.Brush. Actually, .NET platform has two such brushes:

In order to set paint or a stroke in PsDocument we must pass an object of System.Drawing.Brush class for a painting and an object of System.Drawing.Pen for stroking into respective methods. Aspose.Page for .NET library processes all subclasses of System.Drawing.Brush that are offered by the .NET platform. These are System.Drawing.SolidBrush, System.Drawing.TextureBrush, System.Drawing.LinearGradientBrush, System.Drawing.PathGradientBrush and System.Drawing.HatchBrush. System.Drawing.Pen class cannot be extended because it is sealed, but it contains System.Drawing.Brush as a property and, thus, Aspose.Page for .NET library can also use a complete set of brushes also for drawing lines and outlining shapes and text.

In order to paint graphics objects with a gradient in Aspose.Page for .NET library it is necessary to create System.Drawing.LinearGradientBrush or System.Drawing.PathGradientBrush and pass it to SetPaint() or one of the FillText() or FillAndStrokeText() methods which accept System.Drawing.Brush as a parameter.

In order to outline graphics objects with a gradient in Aspose.Page for .NET library someone should create System.Drawing.LinearGradientBrush or System.Drawing.PathGradientBrush, then create System.Drawing.Pen with this brush and, finally, pass it to SetStroke() or one of the OutlineText() or FillAndStrokeText() methods which accepts System.Drawing.Pen as a parameter.

In the example below we demonstrate how to fill a shape and a text and outline the text with a gradient.

An algorithm for painting graphics objects with a gradient in a new PS document includes the following steps:

  1. Create an output stream for the resulting PS file.
  2. Create PsSaveOptions.
  3. Create PsDocument with the already created output stream and save options.
  4. Create the necessary graphics path or font in dependence on what object we are going to fill or outline.
  5. Create an object of System.Drawing.LinearGradientBrush or System.Drawing.PathGradientBrush in dependence on the wishful form of a gradient.
  6. Set the necessary transformation on this brush.
  7. Set the gradient brush as the current paint in PsDocument
  8. Fill the graphics path with current paint or fill a text. If we use one of the methods for filling the text that accepts System.Drawing.Brush as a parameter, the previous point can be ignored.
  9. Close the page.
  10. Save the document.

If we need stroking (outlining) graphics objects with a gradient instead of the last 4 points following will be:

  1. Create the System.Drawing.Pen object with the gradient brush.

  2. Set this pen as the current stroke in PsDocument.

  3. Outline the graphics path with the current stroke or outline the text. If we use one of the methods for outlining the text that accepts System.Drawing.Pen as a parameter, previous point can be ignored.

  4. Close the page.

  5. Save the document.

We offer 5 code snippets that demonstrate a usage of different gradients.

In this code snippet we create horizontal linear gradient from two colors, fill a rectangle, fill a text, outline a text with this gradient.

 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}

For Linux, MacOS and other non-Windows operation systems we offer to use our Aspose.Page.Drawing Nuget package. It uses Aspose.Drawing backend instead of System.Drawing system library.

So import Aspose.Page.Drawing namespace instead of System.Drawing one. In the above and following code snippets Aspose.Page.Drawing.RectangleF will be used instead of System.Drawing.RectangleF, Aspose.Page.Drawing.Drawing2D.GraphicsPath will be used instead of System.Drawing.Drawing2D.GraphicsPath and so on. Our code examples on GitHub contain all the necessary substitutions.

The result of running this code is appeared as

Add Horizontal Gradient

In this code snippet we create a vertical linear gradient from 5 colors and fill a rectangle with this gradient.

 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}

Here comes the result

Add Vertical Gradient

In this code snippet we create a diagonal linear gradient from 2 colors and fill a rectangle with this gradient.

 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}

Here comes the result

Add Diagonal Gradient

In this code snippet we create a radial gradient from 2 colors and fill a circle with this gradient.

 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}

The result

Add Radial Gradient image1

In this code snippet we create a radial gradient from 6 colors and fill a rectangle with this gradient.

 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}

The result

Add Radial Gradient image2

See working with gradient in PS documents in Java.

You can download examples and data files from GitHub.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.