Lavora con gli stati grafici

Ritaglia e trasforma gli stati grafici dei file PS

 

La gestione degli stati grafici nei documenti PS (equivalenti alle tele in XPS) è una delle principali funzionalità offerte da Aspose.Page per .NET. Nell'esempio seguente scoprirai come:

  • Salva lo stato grafico corrente, crea un nuovo stato grafico e impostalo come corrente.

  • Trasla, ridimensiona, ruota o inclina lo stato grafico corrente.

  • Imposta una trasformazione complessa per lo stato grafico corrente.

  • Imposta la pittura e il tratto per lo stato grafico corrente.

  • Compila e disegna un percorso grafico.

  • Ritaglia lo stato della grafica.

  • Ripristina lo stato grafico precedente.

Per trasformare gli stati grafici di un file PS segui la guida successiva:

  1. Creare un file PS utilizzando la PsDocument Class .
  2. Crea un percorso grafico rettangolare.
  3. Salva lo stato grafico corrente, crea un nuovo stato grafico e impostalo come corrente con WriteGraphicsSave() Metodo.
  4. Traduci lo stato grafico corrente utilizzando il metodo Translate() .
  5. Imposta la vernice nello stato grafico corrente con il metodo SetPaint() .
  6. Compilare il percorso grafico mediante il metodo Fill() .
  7. Ripristina lo stato grafico precedente con il metodo WriteGraphicsRestore .
  8. Ripetere i passaggi 3-7 per aggiungere più stati grafici con altre trasformazioni utilizzando Scale() , Ruota() , Shear() e Transform() Metodi.
  9. Chiudere la pagina corrente tramite il metodo ClosePage() .
  10. Salvare il documento PS creato utilizzando il metodo PsDocument.Save() .

Stato grafico del file PS Trasformazione Codice 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();
    }

Per aggiungere una clip allo stato grafico di un file PS segui la guida successiva:

  1. Creare un file PS utilizzando la PsDocument Class .
  2. Crea un percorso grafico rettangolare.
  3. Salva lo stato grafico corrente, crea un nuovo stato grafico e impostalo come corrente con WriteGraphicsSave() Metodo.
  4. Traduci lo stato grafico corrente utilizzando il metodo Translate() .
  5. Crea un percorso grafico circolare.
  6. Aggiungi il ritaglio per cerchio allo stato grafico corrente utilizzando il metodo Clip() .
  7. Imposta la vernice nello stato grafico corrente con il metodo SetPaint() .
  8. Riempire il percorso grafico del rettangolo mediante il metodo Fill() .
  9. Ripristina lo stato grafico precedente con il metodo WriteGraphicsRestore() .
  10. Traduci lo stato grafico corrente utilizzando il metodo Translate() .
  11. Crea un oggetto System.Drawing.Pen.
  12. Imposta un tratto nello stato grafico corrente con il metodo SetStroke() .
  13. Disegna il percorso grafico del rettangolo sopra il rettangolo ritagliato mediante il metodo Draw() .
  14. Chiudere la pagina corrente mediante il metodo ClosePage() .
  15. Salva il documento PS creato utilizzando il metodo PsDocument.Save() .

Codice C# di ritaglio dello stato grafico del file 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. Quali sono gli stati grafici nei documenti PostScript (PS)?

Gli stati grafici in PostScript sono le impostazioni e gli attributi correnti applicati agli elementi grafici all’interno del documento. Includono parametri come la matrice di trasformazione corrente, lo stile della linea, il colore di riempimento, il tracciato di ritaglio e altri attributi grafici che influiscono sul modo in cui gli elementi vengono visualizzati sulla pagina.

2. Come posso gestire gli stati grafici nei documenti PS?

Questo può essere fatto utilizzando comandi PostScript come gsave e grestore rispettivamente per salvare e ripristinare lo stato grafico. Inoltre, operatori come “setlinewidth”, “setrgbcolor” e “setdash” possono essere utilizzati per modificare attributi specifici dello stato grafico secondo necessità.

3. Perché è importante comprendere e gestire gli stati grafici nei documenti PostScript (PS)?

Manipolando gli stati della grafica, è possibile ottenere effetti quali trasformazioni, cambiamenti di colore e aree di ritaglio, garantendo che la grafica venga visualizzata correttamente e in base alle specifiche di progettazione. Aiuta anche a ottimizzare le dimensioni e le prestazioni dei file, in particolare quando si ha a che fare con elementi grafici complessi o ripetitivi.

PS Cos'è il formato file PS

Il formato PS è uno dei formati PDL (Page Description Language). È in grado di contenere informazioni grafiche e di testo sulla pagina. Ecco perché il formato è stato supportato dalla maggior parte dei programmi per l'editing delle immagini. Il file PostScript stesso è una sorta di istruzione per le stampanti. Contiene informazioni su cosa e come stampare dalla sua pagina.