Travailler avec le dégradé dans PostScript | .FILET

Ajouter un dégradé dans un document PS

Dans cet article, nous examinons les manières dont un dégradé peut être utilisé dans les documents PS.

Le dégradé est une transition douce d’une couleur à une autre. Il est utilisé pour rendre les images dessinées plus réalistes. Comme le dégradé est une sorte de peinture, on s’attend à ce qu’il soit implémenté dans .NET en tant que sous-classe de System.Drawing.Brush. En fait, la plate-forme .NET dispose de deux de ces pinceaux :

Afin de définir de la peinture ou un trait dans PsDocument, nous devons passer un objet de la classe System.Drawing.Brush pour une peinture et un objet de System.Drawing.Pen pour le trait dans méthodes respectives. La bibliothèque Aspose.Page pour .NET traite toutes les sous-classes de System.Drawing.Brush proposées par la plateforme .NET. Il s’agit de System.Drawing.SolidBrush, System.Drawing.TextureBrush, System.Drawing.LinearGradientBrush, System.Drawing.PathGradientBrush et *System.Drawing.HatchBrush. *. La classe System.Drawing.Pen ne peut pas être étendue car elle est scellée, mais elle contient System.Drawing.Brush comme propriété et, par conséquent, la bibliothèque Aspose.Page pour .NET peut également utiliser un ensemble complet de pinceaux également pour dessiner des lignes et décrire des formes et du texte.

Afin de peindre des objets graphiques avec un dégradé dans la bibliothèque Aspose.Page pour .NET, il est nécessaire de créer System.Drawing.LinearGradientBrush ou System.Drawing.PathGradientBrush et de le transmettre à SetPaint () ou l’une des méthodes FillText() ou FillAndStrokeText() qui acceptent System.Drawing.Brush comme paramètre.

Afin de décrire des objets graphiques avec un dégradé dans la bibliothèque Aspose.Page pour .NET, quelqu’un doit créer System.Drawing.LinearGradientBrush ou System.Drawing.PathGradientBrush, puis créer System.Drawing. Stylo avec ce pinceau et, enfin, transmettez-le à SetStroke() ou à l’une des méthodes OutlineText() ou FillAndStrokeText() qui accepte *System.Drawing.Pen * comme paramètre.

Dans l’exemple ci-dessous, nous montrons comment remplir une forme et un texte et décrire le texte avec un dégradé.

Un algorithme pour peindre des objets graphiques avec un dégradé dans un nouveau document PS comprend les étapes suivantes :

  1. Créez un flux de sortie pour le fichier PS résultant.
  2. Créez PsSaveOptions.
  3. Créez PsDocument avec le flux de sortie déjà créé et enregistrez les options.
  4. Créez le chemin graphique ou la police nécessaire en fonction de l’objet que nous allons remplir ou décrire.
  5. Créez un objet de System.Drawing.LinearGradientBrush ou System.Drawing.PathGradientBrush en fonction de la forme pieux d’un dégradé.
  6. Définissez la transformation nécessaire sur ce pinceau.
  7. Définissez le pinceau dégradé comme peinture actuelle dans PsDocument
  8. Remplissez le chemin graphique avec la peinture actuelle ou remplissez un texte. Si nous utilisons l’une des méthodes de remplissage du texte qui accepte System.Drawing.Brush comme paramètre, le point précédent peut être ignoré.
  9. Fermez la page.
  10. Enregistrez le document.

Si nous avons besoin de tracer (décrire) des objets graphiques avec un dégradé au lieu des 4 derniers points, ce qui suit sera :8. Créez l’objet System.Drawing.Pen avec le pinceau dégradé.

  1. Définissez ce stylet comme trait actuel dans PsDocument.

  2. Décrivez le chemin graphique avec le trait actuel ou décrivez le texte. Si nous utilisons l’une des méthodes pour décrire le texte qui accepte System.Drawing.Pen comme paramètre, le point précédent peut être ignoré.

  3. Fermez la page.

  4. Enregistrez le document.

Nous proposons 5 extraits de code qui démontrent l’utilisation de différents dégradés.

Dans cet extrait de code, nous créons un dégradé linéaire horizontal à partir de deux couleurs, remplissons un rectangle, remplissons un texte, décrivons un texte avec ce dégradé.

 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}

Pour Linux, MacOS et autres systèmes d’exploitation non Windows, nous proposons d’utiliser notre package Nuget Aspose.Page.Drawing. Il utilise le backend Aspose.Drawing au lieu de la bibliothèque système System.Drawing.

Importez donc l’espace de noms Aspose.Page.Drawing au lieu de celui de System.Drawing. Dans les extraits de code ci-dessus et suivants, Aspose.Page.Drawing.RectangleF sera utilisé à la place de System.Drawing.RectangleF, Aspose.Page.Drawing.Drawing2D.GraphicsPath sera utilisé à la place de System.Drawing.Drawing2D.GraphicsPath et ainsi de suite. Nos exemples de code sur GitHub contiennent toutes les substitutions nécessaires.

Le résultat de l’exécution de ce code apparaît comme

Ajouter un dégradé horizontal

Dans cet extrait de code, nous créons un dégradé linéaire vertical à partir de 5 couleurs et remplissons un rectangle avec ce dégradé.

 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}

Voici le résultat

Ajouter un dégradé vertical

Dans cet extrait de code, nous créons un dégradé linéaire diagonal à partir de 2 couleurs et remplissons un rectangle avec ce dégradé.

 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}

Voici le résultat

Ajouter un dégradé diagonal

Dans cet extrait de code, nous créons un dégradé radial à partir de 2 couleurs et remplissons un cercle avec ce dégradé.

 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}

Le résultat

Ajouter une image de dégradé radial1

Dans cet extrait de code, nous créons un dégradé radial à partir de 6 couleurs et remplissons un rectangle avec ce dégradé.

 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}

Le résultat

Ajouter une image de dégradé radial2

Voir Travailler avec un dégradé dans les documents PS dans Java.

Vous pouvez télécharger des exemples et des fichiers de données à partir de GitHub.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.