Trabajar con degradado en PostScript | .NET

Agregar degradado en documento PS

En este artículo, consideramos las formas en que se puede utilizar un degradado en documentos PS.

El degradado es una transición suave de un color a otro. Se utiliza para hacer que los dibujos dibujados sean más realistas. Como el degradado es un tipo de pintura, se espera que en .NET se implemente como una subclase de System.Drawing.Brush. En realidad, la plataforma .NET tiene dos de estos pinceles:

Para configurar la pintura o un trazo en PsDocument debemos pasar un objeto de la clase System.Drawing.Brush para una pintura y un objeto de System.Drawing.Pen para aplicar un trazo. métodos respectivos. La biblioteca Aspose.Page para .NET procesa todas las subclases de System.Drawing.Brush que ofrece la plataforma .NET. Estos son System.Drawing.SolidBrush, System.Drawing.TextureBrush, System.Drawing.LinearGradientBrush, System.Drawing.PathGradientBrush y System.Drawing.HatchBrush. La clase System.Drawing.Pen no se puede ampliar porque está sellada, pero contiene System.Drawing.Brush como propiedad y, por lo tanto, la biblioteca Aspose.Page para .NET también puede usar un conjunto completo de pinceles también para dibujar líneas y delinear formas y texto.

Para pintar objetos gráficos con un degradado en la biblioteca Aspose.Page para .NET es necesario crear System.Drawing.LinearGradientBrush o System.Drawing.PathGradientBrush y pasarlo a SetPaint () o uno de los métodos FillText() o FillAndStrokeText() que aceptan System.Drawing.Brush como parámetro.

Para delinear objetos gráficos con un degradado en Aspose.Page para la biblioteca .NET, alguien debe crear System.Drawing.LinearGradientBrush o System.Drawing.PathGradientBrush, luego crear System.Drawing. Rotula con este pincel y, finalmente, pásalo a SetStroke() o a uno de los métodos OutlineText() o FillAndStrokeText() que acepte *System.Drawing.Pen. * como parámetro.

En el siguiente ejemplo, demostramos cómo rellenar una forma y un texto y delinear el texto con un degradado.

Un algoritmo para pintar objetos gráficos con un degradado en un nuevo documento PS incluye los siguientes pasos:

  1. Cree una secuencia de salida para el archivo PS resultante.
  2. Cree PsSaveOptions.
  3. Cree PsDocument con el flujo de salida ya creado y guarde las opciones.
  4. Cree la ruta gráfica o la fuente necesaria según el objeto que vayamos a rellenar o delinear.
  5. Cree un objeto de System.Drawing.LinearGradientBrush o System.Drawing.PathGradientBrush dependiendo de la forma deseada de un degradado.
  6. Establezca la transformación necesaria en este pincel.
  7. Establezca el pincel de degradado como pintura actual en PsDocument.
  8. Rellene la ruta de los gráficos con la pintura actual o rellene un texto. Si utilizamos uno de los métodos para rellenar el texto que acepta System.Drawing.Brush como parámetro, se puede ignorar el punto anterior.
  9. Cierra la página.
  10. Guarde el documento.

Si necesitamos trazar (delinear) objetos gráficos con un degradado en lugar de los últimos 4 puntos, lo siguiente será:8. Cree el objeto System.Drawing.Pen con el pincel de degradado.

  1. Establezca este lápiz como trazo actual en PsDocument.

  2. Delinee la ruta de los gráficos con el trazo actual o delinee el texto. Si utilizamos uno de los métodos para delinear el texto que acepta System.Drawing.Pen como parámetro, se puede ignorar el punto anterior.

  3. Cierra la página.

  4. Guarde el documento.

Ofrecemos 5 fragmentos de código que demuestran el uso de diferentes gradientes.

En este fragmento de código creamos un degradado lineal horizontal a partir de dos colores, rellenamos un rectángulo, rellenamos un texto y delineamos un texto con este degradado.

 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();

Para Linux, MacOS y otros sistemas operativos distintos de Windows, ofrecemos utilizar nuestro paquete Nuget Aspose.Page.Drawing. Utiliza el backend Aspose.Drawing en lugar de la biblioteca del sistema System.Drawing.

Así que importe el espacio de nombres Aspose.Page.Drawing en lugar de System.Drawing. En los fragmentos de código anteriores y siguientes se utilizará Aspose.Page.Drawing.RectangleF en lugar de System.Drawing.RectangleF, se utilizará Aspose.Page.Drawing.Drawing2D.GraphicsPath en lugar de System.Drawing.Drawing2D.GraphicsPath y así sucesivamente. Nuestros ejemplos de código en GitHub contienen todas las sustituciones necesarias.

El resultado de ejecutar este código aparece como

Agregar degradado horizontal

En este fragmento de código creamos un degradado lineal vertical a partir de 5 colores y rellenamos un rectángulo con este degradado.

 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();

Aquí viene el resultado

Agregar degradado vertical

En este fragmento de código creamos un degradado lineal diagonal a partir de 2 colores y rellenamos un rectángulo con este degradado.

 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();
Aquí viene el resultado

Agregar degradado diagonal

En este fragmento de código creamos un degradado radial a partir de 2 colores y rellenamos un círculo con este degradado.

 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();

el resultado

Agregar imagen de degradado radial1

En este fragmento de código creamos un degradado radial a partir de 6 colores y rellenamos un rectángulo con este degradado.

 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();

el resultado

Agregar imagen de degradado radial2

Consulte cómo trabajar con degradados en documentos PS en Java.

Puede descargar ejemplos y archivos de datos desde GitHub.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.