Làm việc với các trạng thái đồ họa

Cắt và chuyển đổi trạng thái đồ họa của tệp PS

 

Quản lý trạng thái đồ họa trong tài liệu PS (tương đương với canvas trong XPS) là một trong những tính năng chính được Aspose.Page cung cấp cho .NET. Trong ví dụ dưới đây, bạn sẽ tìm hiểu cách:

  • Lưu trạng thái đồ họa hiện tại, tạo trạng thái đồ họa mới và đặt nó như hiện tại.

  • Dịch, chia tỷ lệ, xoay hoặc cắt trạng thái đồ họa hiện tại.

  • Đặt một phép biến đổi phức tạp cho trạng thái đồ họa hiện tại.

  • Đặt màu vẽ và nét vẽ cho trạng thái đồ họa hiện tại.

  • Điền và vẽ đường dẫn đồ họa.

  • Clip trạng thái đồ họa.

  • Khôi phục trạng thái đồ họa trước đó.

Để chuyển đổi trạng thái đồ họa của tệp PS, hãy làm theo hướng dẫn tiếp theo:

  1. Tạo tệp PS bằng cách sử dụng PsDocument Class .
  2. Tạo một đường dẫn đồ họa hình chữ nhật.
  3. Lưu trạng thái đồ họa hiện tại, tạo trạng thái đồ họa mới và đặt trạng thái đó như hiện tại bằng WriteGraphicsSave() Phương pháp.
  4. Dịch trạng thái đồ họa hiện tại bằng Phương thức Translate() .
  5. Đặt màu vẽ ở trạng thái đồ họa hiện tại bằng Phương thức SetPaint() .
  6. Điền vào đường dẫn đồ họa bằng phương thức Fill() .
  7. Khôi phục trạng thái đồ họa trước đó bằng Phương thức WriteGraphicsRestore .
  8. Lặp lại các bước 3-7 để thêm các trạng thái đồ họa khác bằng các phép biến đổi khác bằng cách sử dụng Scale() , Rotate() , Shear()Transform() Phương pháp.
  9. Đóng trang hiện tại bằng phương thức ClosePage() .
  10. Lưu tài liệu PS đã tạo bằng Phương thức PsDocument.Save() .

Trạng thái đồ họa tệp PS Chuyển đổi mã 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();
    }

Để thêm Clip vào trạng thái đồ họa của tệp PS, hãy làm theo hướng dẫn tiếp theo:

  1. Tạo tệp PS bằng cách sử dụng PsDocument Class .
  2. Tạo một đường dẫn đồ họa hình chữ nhật.
  3. Lưu trạng thái đồ họa hiện tại, tạo trạng thái đồ họa mới và đặt trạng thái đó như hiện tại bằng WriteGraphicsSave() Phương pháp.
  4. Dịch trạng thái đồ họa hiện tại bằng Phương thức Translate() .
  5. Tạo một đường dẫn đồ họa hình tròn.
  6. Thêm phần cắt theo vòng tròn vào trạng thái đồ họa hiện tại bằng cách sử dụng Phương thức Clip() .
  7. Đặt màu vẽ ở trạng thái đồ họa hiện tại bằng Phương thức SetPaint() .
  8. Điền vào đường dẫn đồ họa hình chữ nhật bằng Phương thức Fill() .
  9. Khôi phục trạng thái đồ họa trước đó bằng Phương thức WriteGraphicsRestore() .
  10. Dịch trạng thái đồ họa hiện tại bằng Phương thức Translate() .
  11. Tạo một đối tượng System.Drawing.Pen.
  12. Đặt nét vẽ ở trạng thái đồ họa hiện tại bằng Phương thức SetStroke() .
  13. Vẽ đường dẫn đồ họa hình chữ nhật phía trên hình chữ nhật được cắt bớt bằng Phương thức Draw() .
  14. Đóng trang hiện tại bằng Phương thức ClosePage() .
  15. Lưu tài liệu PS đã tạo bằng Phương thức PsDocument.Save() .

Trạng thái đồ họa tệp PS cắt mã C#

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



Câu hỏi thường gặp

1. Trạng thái đồ họa trong tài liệu PostScript (PS) là gì?

Trạng thái đồ họa trong PostScript là các cài đặt và thuộc tính hiện tại được áp dụng cho các thành phần đồ họa trong tài liệu. Chúng bao gồm các tham số như ma trận chuyển đổi hiện tại, kiểu đường kẻ, màu tô, đường cắt và các thuộc tính đồ họa khác ảnh hưởng đến cách các phần tử được hiển thị trên trang.

2. Làm cách nào tôi có thể quản lý trạng thái đồ họa trong tài liệu PS?

Điều này có thể được thực hiện bằng cách sử dụng các lệnh PostScript như gsavegrestore để lưu và khôi phục trạng thái đồ họa tương ứng. Ngoài ra, các toán tử như setlinewidth, setrgbcolorsetdash có thể được sử dụng để sửa đổi các thuộc tính cụ thể của trạng thái đồ họa nếu cần.

3. Tại sao việc hiểu và quản lý trạng thái đồ họa trong tài liệu PostScript (PS) lại quan trọng?

Bằng cách thao tác các trạng thái đồ họa, bạn có thể đạt được các hiệu ứng như chuyển đổi, thay đổi màu sắc và cắt vùng, đảm bảo rằng đồ họa của bạn được hiển thị chính xác và theo thông số thiết kế của bạn. Nó cũng giúp tối ưu hóa kích thước và hiệu suất tệp, đặc biệt khi xử lý các phần tử đồ họa phức tạp hoặc lặp đi lặp lại.

PS Những gì là PS Tập Tin Định Dạng

Định dạng PS là một trong những định dạng ngôn ngữ mô tả trang (PDL). Nó có khả năng chứa thông tin đồ họa cũng như văn bản trên trang. Đó là lý do tại sao định dạng được hỗ trợ bởi hầu hết các chương trình để chỉnh sửa hình ảnh. Bản thân tệp tái bút là một loại hướng dẫn cho máy in. Nó chứa thông tin về cái gì và cách in từ trang của nó.