Working with Textures in PostScript | .NET

Add Texture Tiling Pattern in PS Document

A texture tiling pattern is an image that is used for filling or drawing objects: shapes or text. If the size of the image is less than the size of the object it is repeated in X and Y directions for covering all necessary areas.

The process of repetition of the image inside graphics objects is called tiling. In order to set paint or stroke in PsDocument we must pass an object of System.Drawing.Brush class for a painting and an object of System.Drawing.Pen for stroking into respective methods.

Aspose.Page for .NET library processes all subclasses of System.Drawing.Brush that is offered by the .NET platform. These are System.Drawing.SolidBrush, System.Drawing.TextureBrush, System.Drawing.LinearGradientBrush, System.Drawing.PathGradientBrush and System.Drawing.HatchBrush. System.Drawing.Pen class cannot be extended because it is sealed, but it contains System.Drawing.Brush as a property and, thus, Aspose.Page for .NET library can also use a complete set of brushes also for drawing lines and outlining shapes and text.

In order to paint graphics objects with a textured pattern in Aspose.Page for .NET library it is enough simply to pass System.Drawing.TextureBrush to SetPaint() or one of the FillText() or FillAndStrokeText() methods which accepts System.Drawing.Brush as a parameter.
In order to outline graphics objects with a textured pattern in Aspose.Page for .NET library you should create new System.Drawing.Pen with System.Drawing.TextureBrush and pass it to SetStroke() or one of the OutlineText() or FillAndStrokeText() methods which accepts System.Drawing.Pen as a parameter.

In the example below we demonstrate how to fill a shape and a text and outline a text with a texture tiling pattern.

Description of steps of working with Texture Pattern and PsDocument in the example:

  1. Create an output stream for the resulting PS file.
  2. Create PsSaveOptions object with default options.
  3. Create a 1-paged PsDocument with an already created output stream and save options.
  4. Create a new graphics state and translate it to the necessary position.
  5. Create System.Drawing.Bitmap from image file.
  6. Create System.Drawing.TextureBrush from the image.
  7. Set the necessary transformation in the texture brush.
  8. Set the texture brush as current paint in the current graphics state of PsDocument.
  9. Create a rectangle path.
  10. Fill the rectangle with the texture brush.
  11. Save current paint as a local variable for future use.
  12. Set the current stroke with a red pen.
  13. Outline the rectangle with a current pen.
  14. Exit from the current graphics state to the upper-level graphics state.
  15. Create system font.
  16. Fill and stroke (outline) text. For filling the texture brush is used, and for stroking black pen is used.
  17. Outline the text in the other position with the new System.Drawing.Pen created with texture brush as Brush.
  18. Close the page.
  19. Save the document.
 1 //Create an output stream for PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "AddTextureTilingPattern_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    
11    document.WriteGraphicsSave();
12    document.Translate(200, 100);
13
14    //Create a Bitmap object from image file
15    using (Bitmap image = new Bitmap(dataDir + "TestTexture.bmp"))
16    {
17        //Create texture brush from the image
18        TextureBrush brush = new TextureBrush(image, WrapMode.Tile);
19
20        //Add scaling in X direction to the mattern
21        System.Drawing.Drawing2D.Matrix transform = new System.Drawing.Drawing2D.Matrix(2, 0, 0, 1, 0, 0);
22        brush.Transform = transform;
23
24        //Set this texture brush as current paint
25        document.SetPaint(brush);
26    }
27
28    //Create a rectangle path
29    System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
30    path.AddRectangle(new System.Drawing.RectangleF(0, 0, 200, 100));
31
32    //Fill the rectangle
33    document.Fill(path);
34
35    //Get current paint
36    Brush paint = document.GetPaint();
37
38    //Set red stroke
39    document.SetStroke(new Pen(new SolidBrush(Color.Red), 2));
40    //Stroke the rectangle
41    document.Draw(path);
42
43    document.WriteGraphicsRestore();
44
45    //Fill the text with the texture pattern                
46    System.Drawing.Font font = new System.Drawing.Font("Arial", 96, FontStyle.Bold);
47    document.FillAndStrokeText("ABC", font, 200, 300, paint, new Pen(Color.Black, 2));
48
49    //Outline the text with the texture pattern
50    document.OutlineText("ABC", font, 200, 400, new Pen(paint, 5));
51
52    //Close current page
53    document.ClosePage();
54
55    //Save the document
56    document.Save();
57}

See working with textures in PS documents in Java and C++.


The result of running this code is appeared as

Add Texture Tiling Pattern

You can download examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.