Travailler avec les états graphiques

Découper et transformer les états graphiques des fichiers PS

 

La gestion des états graphiques dans les documents PS (équivalent des canevas dans XPS) est l'une des principales fonctionnalités proposées par Aspose.Page pour .NET. Dans l'exemple ci-dessous, vous découvrirez comment :

  • Enregistrez l'état graphique actuel, créez un nouvel état graphique et définissez-le comme actuel.

  • Traduisez, mettez à l'échelle, faites pivoter ou ciselez l'état graphique actuel.

  • Définissez une transformation complexe pour l'état graphique actuel.

  • Définissez la peinture et le trait pour l'état graphique actuel.

  • Remplissez et dessinez un chemin graphique.

  • Découpez l'état graphique.

  • Restaurez l'état graphique précédent.

Pour transformer les états graphiques d'un fichier PS, suivez le guide suivant :

  1. Créez un fichier PS à l’aide de la PsDocument Class .
  2. Créez un chemin graphique rectangulaire.
  3. Enregistrez l’état graphique actuel, créez un nouvel état graphique et définissez-le comme actuel avec WriteGraphicsSave() Méthode.
  4. Traduisez l’état graphique actuel à l’aide de la méthode Translate() .
  5. Définissez la peinture dans l’état graphique actuel avec la méthode SetPaint() .
  6. Remplissez le chemin graphique au moyen de la méthode Fill() .
  7. Restaurez l’état graphique précédent avec la méthode WriteGraphicsRestore .
  8. Répétez les étapes 3 à 7 pour ajouter d’autres états graphiques avec d’autres transformations en utilisant Scale() , Rotation() , Shear() et Transform() Méthodes.
  9. Fermez la page actuelle au moyen de la méthode ClosePage() .
  10. Enregistrez le document PS créé à l’aide de la méthode PsDocument.Save() .

État graphique du fichier PS Transformation Code C#

    using Aspose.Page.EPS;
    using Aspose.Page.EPS.Device;    
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.IO;
    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithCanvas();

    //Create output stream for PostScript document
    using (Stream outPsStream = new FileStream(dataDir + "Transformations_outPS.ps", FileMode.Create))
    {
        //Create save options with default values
        PsSaveOptions options = new PsSaveOptions();

        // Create new 1-paged PS Document
        PsDocument document = new PsDocument(outPsStream, options, false);
        
        //Create graphics path from the rectangle
        System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
        path.AddRectangle(new System.Drawing.RectangleF(0, 0, 150, 100));
            
    		///////////////////// Translation //////////////////////////////////////////////////////////////////////

        //Save graphics state in order to return back to this state after transformation
        document.WriteGraphicsSave();

        //Displace current graphics state on 250 to the right. So we add translation component to the current transformation.
        document.Translate(250, 0);

        //Set paint in the current graphics state
        document.SetPaint(new System.Drawing.SolidBrush(Color.Blue));

        //Fill the second rectangle in the current graphics state (has translation transformation)
        document.Fill(path);

        //Restore graphics state to the previus (upper) level
        document.WriteGraphicsRestore();
				/////////////////////////////////////////////////////////////////////////////////////////////////////////


        //Displace on 200 to the bottom.
        document.Translate(0, 200);

				////////////////////// Scaling //////////////////////////////////////////////////////////////////////////
        //Save graphics state in order to return back to this state after transformation
        document.WriteGraphicsSave();

        //Scale current graphics state on 0.5 in X axis and on 0.75f in Y axis. So we add scale component to the current transformation.
        document.Scale(0.5f, 0.75f);

        //Set paint in the current graphics state
        document.SetPaint(new System.Drawing.SolidBrush(Color.Red));

        //Fill the third rectangle in the current graphics state (has scale transformation)
        document.Fill(path);

        //Restore graphics state to the previus (upper) level
        document.WriteGraphicsRestore();
				//////////////////////////////////////////////////////////////////////////////////////////////////////


        //Displace upper level graphics state on 250 to the right.
        document.Translate(250, 0);


				////////////////////// Rotation //////////////////////////////////////////////////////////////////////
        //Save graphics state in order to return back to this state after transformation
        document.WriteGraphicsSave();

        //Rotate current graphics state on 45 degrees around origin of current graphics state (350, 300). So we add rotation component to the current transformation.
        document.Rotate(45);

        //Set paint in the current graphics state
        document.SetPaint(new System.Drawing.SolidBrush(Color.Green));

        //Fill the fourth rectangle in the current graphics state (has rotation transformation)
        document.Fill(path);

        //Restore graphics state to the previus (upper) level
        document.WriteGraphicsRestore();
				//////////////////////////////////////////////////////////////////////////////////////////////////////


        //Returns upper level graphics state back to the left and displace on 200 to the bottom.
        document.Translate(-250, 200);


				////////////////////// Shearing //////////////////////////////////////////////////////////////////////
        //Save graphics state in order to return back to this state after transformation
        document.WriteGraphicsSave();

        //Shear current graphics state. So we add shear component to the current transformation.
        document.Shear(0.1f, 0.2f);

        //Set paint in the current graphics state
        document.SetPaint(new System.Drawing.SolidBrush(Color.Pink));

        //Fill the fifth rectangle in the current graphics state (has shear transformation)
        document.Fill(path);

        //Restore graphics state to the previus (upper) level
        document.WriteGraphicsRestore();
				//////////////////////////////////////////////////////////////////////////////////////////////////////


        //Displace upper level graphics state on 250 to the right.
        document.Translate(250, 0);


				////////////////////// Complex transformation ////////////////////////////////////////////////////////
        //Save graphics state in order to return back to this state after transformation
        document.WriteGraphicsSave();

        //Transform current graphics state with complex transformation. So we add translation, scale and rotation components to the current transformation.
        document.Transform(new System.Drawing.Drawing2D.Matrix(1.2f, -0.965925f, 0.258819f, 1.5f, 0f, 50));

        //Set paint in the current graphics state
        document.SetPaint(new System.Drawing.SolidBrush(Color.Aquamarine));

        //Fill the sixth rectangle in the current graphics state (has complex transformation)
        document.Fill(path);

        //Restore graphics state to the previus (upper) level
        document.WriteGraphicsRestore();
				//////////////////////////////////////////////////////////////////////////////////////////////////////
				
        //Close current page
        document.ClosePage();

        //Save the document
        document.Save();
    }

Pour ajouter un clip à l'état graphique d'un fichier PS, suivez le guide suivant :

  1. Créez un fichier PS à l’aide de la PsDocument Class .
  2. Créez un chemin graphique rectangulaire.
  3. Enregistrez l’état graphique actuel, créez un nouvel état graphique et définissez-le comme actuel avec WriteGraphicsSave() Méthode.
  4. Traduisez l’état graphique actuel à l’aide de la méthode Translate() .
  5. Créez un chemin graphique circulaire.
  6. Ajoutez un découpage par cercle à l’état graphique actuel à l’aide de la méthode Clip() .
  7. Définissez la peinture dans l’état graphique actuel avec la méthode SetPaint() .
  8. Remplissez le chemin graphique du rectangle au moyen de la méthode Fill() .
  9. Restaurez l’état graphique précédent avec la méthode WriteGraphicsRestore() .
  10. Traduisez l’état graphique actuel à l’aide de la méthode Translate() .
  11. Créez un objet System.Drawing.Pen.
  12. Définissez un trait dans l’état graphique actuel avec la méthode SetStroke() .
  13. Dessinez le chemin graphique du rectangle au-dessus du rectangle découpé au moyen de la méthode Draw() .
  14. Fermez la page actuelle au moyen de la méthode ClosePage() .
  15. Enregistrez le document PS créé à l’aide de la méthode PsDocument.Save() .

Code C# de découpage de l'état graphique du fichier PS

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithCanvas();

    //Create output stream for PostScript document
    using (Stream outPsStream = new FileStream(dataDir + "Clipping_outPS.ps", FileMode.Create))
    {
        //Create save options with default values
        PsSaveOptions options = new PsSaveOptions();

        // Create new 1-paged PS Document
        PsDocument document = new PsDocument(outPsStream, options, false);

        //Create graphics path from the rectangle
        System.Drawing.Drawing2D.GraphicsPath rectangePath = new System.Drawing.Drawing2D.GraphicsPath();
        rectangePath.AddRectangle(new System.Drawing.RectangleF(0, 0, 300, 200));

				////////////////////// Clipping by shape //////////////////////////////////////////////////////////////////////

        //Save graphics state in order to return back to this state after transformation
        document.WriteGraphicsSave();

        //Displace current graphics state on 100 points to the right and 100 points to the bottom.
        document.Translate(100, 100);

        //Create graphics path from the circle
        System.Drawing.Drawing2D.GraphicsPath circlePath = new System.Drawing.Drawing2D.GraphicsPath();
        circlePath.AddEllipse(new System.Drawing.RectangleF(50, 0, 200, 200));

        //Add clipping by circle to the current graphics state
        document.Clip(circlePath);

        //Set paint in the current graphics state
        document.SetPaint(new System.Drawing.SolidBrush(Color.Blue));

        //Fill the rectangle in the current graphics state (with clipping)
        document.Fill(rectangePath);

        //Restore graphics state to the previus (upper) level
        document.WriteGraphicsRestore();

        //Displace upper level graphics state on 100 points to the right and 100 points to the bottom.
        document.Translate(100, 100);

        Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
        pen.DashStyle = DashStyle.Dash;

        document.SetStroke(pen);

        //Draw the rectangle in the current graphics state (has no clipping) above clipped rectngle
        document.Draw(rectangePath);

				/////////////////////////////////////////////////////////////////////////////////////////////////////////

        //Close current page
        document.ClosePage();

        //Save the document
        document.Save();
    }



FAQ

1. Que sont les états graphiques dans les documents PostScript (PS) ?

Les états graphiques dans PostScript sont les paramètres et attributs actuels appliqués aux éléments graphiques du document. Ils incluent des paramètres tels que la matrice de transformation actuelle, le style de ligne, la couleur de remplissage, le chemin de détourage et d’autres attributs graphiques qui affectent la façon dont les éléments sont rendus sur la page.

2. Comment puis-je gérer les états graphiques dans les documents PS ?

Cela peut être fait en utilisant des commandes PostScript telles que « gsave » et « grestore » pour enregistrer et restaurer l’état graphique, respectivement. De plus, des opérateurs tels que « setlinewidth », « setrgbcolor » et « setdash » peuvent être utilisés pour modifier des attributs spécifiques de l’état graphique selon les besoins.

3. Pourquoi est-il important de comprendre et de gérer les états graphiques dans les documents PostScript (PS) ?

En manipulant les états graphiques, vous pouvez obtenir des effets tels que des transformations, des changements de couleur et des zones de découpage, garantissant ainsi que vos graphiques s’affichent correctement et conformément à vos spécifications de conception. Il permet également d’optimiser la taille et les performances des fichiers, en particulier lorsqu’il s’agit d’éléments graphiques complexes ou répétitifs.

PS Qu'est-ce que le format de fichier PS

Le format PS est l'un des formats de langage de description de page (PDL). Il est capable de contenir des informations graphiques et textuelles sur la page. C'est pourquoi le format était pris en charge par la plupart des programmes de retouche d'image. Le fichier postscript lui-même est une sorte d'instruction pour les imprimeurs. Il contient des informations sur quoi et comment imprimer à partir de sa page.