Arbeiten Sie mit Grafikzuständen

Beschneiden und transformieren Sie den Grafikstatus von PS-Dateien

 

Die Verwaltung von Grafikzuständen in PS-Dokumenten (entspricht den Leinwänden in XPS) ist eine der Hauptfunktionen von Aspose.Page für .NET. Im folgenden Beispiel erfahren Sie, wie Sie:

  • Speichern Sie den aktuellen Grafikstatus, erstellen Sie einen neuen Grafikstatus und legen Sie ihn als aktuell fest.

  • Verschieben, skalieren, drehen oder scheren Sie den aktuellen Grafikstatus.

  • Legen Sie eine komplexe Transformation für den aktuellen Grafikstatus fest.

  • Legen Sie Farbe und Strich für den aktuellen Grafikstatus fest.

  • Füllen und zeichnen Sie einen Grafikpfad.

  • Beschneiden Sie den Grafikstatus.

  • Stellen Sie den vorherigen Grafikstatus wieder her.

Um den Grafikstatus einer PS-Datei umzuwandeln, folgen Sie der nächsten Anleitung:

  1. Erstellen Sie eine PS-Datei mit der PsDocument-Klasse .
  2. Erstellen Sie einen rechteckigen Grafikpfad.
  3. Speichern Sie den aktuellen Grafikstatus, erstellen Sie einen neuen Grafikstatus und legen Sie ihn mit WriteGraphicsSave() als aktuell fest Methode.
  4. Übersetzen Sie den aktuellen Grafikstatus mit der Methode Translate() .
  5. Stellen Sie die Farbe mit der Methode SetPaint() auf den aktuellen Grafikstatus ein.
  6. Füllen Sie den Grafikpfad mit der Methode Fill() .
  7. Stellen Sie den vorherigen Grafikstatus mit der Methode WriteGraphicsRestore wieder her.
  8. Wiederholen Sie die Schritte 3–7, um weitere Grafikzustände mit anderen Transformationen hinzuzufügen, indem Sie Scale() verwenden. Rotate() , Shear() und Transform() Methoden.
  9. Schließen Sie die aktuelle Seite mit der Methode ClosePage() .
  10. Speichern Sie das erstellte PS-Dokument mit der Methode PsDocument.Save() .

PS-Datei-Grafikstatus Transformation C#-Code

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

Um einen Clip zum Grafikstatus einer PS-Datei hinzuzufügen, folgen Sie der nächsten Anleitung:

  1. Erstellen Sie eine PS-Datei mit der PsDocument-Klasse .
  2. Erstellen Sie einen rechteckigen Grafikpfad.
  3. Speichern Sie den aktuellen Grafikstatus, erstellen Sie einen neuen Grafikstatus und legen Sie ihn mit WriteGraphicsSave() als aktuell fest Methode.
  4. Übersetzen Sie den aktuellen Grafikstatus mit der Methode Translate() .
  5. Erstellen Sie einen kreisförmigen Grafikpfad.
  6. Fügen Sie mithilfe der Methode Clip() einen Kreisausschnitt zum aktuellen Grafikstatus hinzu.
  7. Stellen Sie die Farbe mit der Methode SetPaint() auf den aktuellen Grafikstatus ein.
  8. Füllen Sie den rechteckigen Grafikpfad mit der Methode Fill() .
  9. Stellen Sie den vorherigen Grafikstatus mit der Methode WriteGraphicsRestore() wieder her.
  10. Übersetzen Sie den aktuellen Grafikstatus mit der Methode Translate() .
  11. Erstellen Sie ein System.Drawing.Pen-Objekt.
  12. Legen Sie mit der Methode SetStroke() einen Strich im aktuellen Grafikstatus fest.
  13. Zeichnen Sie mit der Methode Draw() den Rechteck-Grafikpfad über dem abgeschnittenen Rechteck.
  14. Schließen Sie die aktuelle Seite mit der Methode ClosePage() .
  15. Speichern Sie das erstellte PS-Dokument mit der Methode PsDocument.Save() .

PS-Dateigrafiken beschneiden den C#-Code mit Status

    // 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. Was sind Grafikzustände in PostScript-Dokumenten (PS)?

Grafikzustände in PostScript sind die aktuellen Einstellungen und Attribute, die auf grafische Elemente im Dokument angewendet werden. Dazu gehören Parameter wie die aktuelle Transformationsmatrix, der Linienstil, die Füllfarbe, der Beschneidungspfad und andere Grafikattribute, die sich darauf auswirken, wie Elemente auf der Seite gerendert werden.

2. Wie kann ich den Grafikstatus in PS-Dokumenten verwalten?

Dies kann mithilfe von PostScript-Befehlen wie „gsave“ und „grestore“ erfolgen, um den Grafikstatus zu speichern bzw. wiederherzustellen. Darüber hinaus können Operatoren wie „setlinewidth“, „setrgbcolor“ und „setdash“ verwendet werden, um bestimmte Attribute des Grafikstatus nach Bedarf zu ändern.

3. Warum ist es wichtig, den Grafikstatus in PostScript-Dokumenten (PS) zu verstehen und zu verwalten?

Durch die Bearbeitung von Grafikzuständen können Sie Effekte wie Transformationen, Farbänderungen und Ausschneidebereiche erzielen und so sicherstellen, dass Ihre Grafiken korrekt und gemäß Ihren Designvorgaben angezeigt werden. Es hilft auch, die Dateigröße und Leistung zu optimieren, insbesondere beim Umgang mit komplexen oder sich wiederholenden grafischen Elementen.

PS Was ist PS Dateiformat

Das PS-Format ist eines der Formate der Seitenbeschreibungssprache (PDL). Es kann sowohl grafische als auch Textinformationen auf der Seite enthalten. Deshalb wurde das Format von den meisten Programmen zur Bildbearbeitung unterstützt. Die Postscript-Datei selbst ist eine Art Anleitung für Drucker. Es enthält Informationen darüber, was und wie von seiner Seite aus gedruckt werden kann.