Working with Images in PostScript | .NET

Contents
[ Hide Show ]

Add Image in PS Document

Aspose.Page for .NET library offers two methods for adding images to PS document:

It was made because PostScript doesn’t support transparency, but translucent images, however, can be rendered as a set of fully transparent and fully opaque pixels. Such images are called masks. If we want to see the translucent image in the PS document as mask, that will better reflect the transparency of the image, we should do some checking and preprocessing of such an image. The check and the preprocessing require time. Therefore, if someone’s sure that the image is fully opaque, it is better to use the first method, because it saves execution time.

The second method recognizes whether the image is fully opaque or fully transparent or translucent. If it is fully opaque it is added as the opaque image in the first method, if it is fully transparent it is not added to the document at all, if it is the translucent image it is added as a PostScript image mask.

In the example below we demonstrate how to add a fully opaque image. Adding a transparent image will be demonstrated in the “Working with Transparency” article.

In order to add an image to a new PsDocument with Aspose.Page for .NET library in this example we take the following steps:

  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.
  5. Create System.Drawing.Bitmap from image file.
  6. Create the necessary transformation for the image.
  7. Add the image to the PsDocument object.
  8. Exit from the current graphics state to upper level one.
  9. Close the page.
  10. Save the document.
 1//Create an output stream for PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "AddImage_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(100, 100);
13
14    //Create a Bitmap object from image file
15    using (Bitmap image = new Bitmap(dataDir + "TestImage Format24bppRgb.jpg"))
16    {
17        //Create image transform
18        System.Drawing.Drawing2D.Matrix transform = new System.Drawing.Drawing2D.Matrix();
19        transform.Translate(35, 300);
20        transform.Scale(3, 3);
21        transform.Rotate(-45);
22
23        //Add the image to document
24        document.DrawImage(image, transform, Color.Empty);
25    }
26
27    document.WriteGraphicsRestore();
28
29    //Close current page
30    document.ClosePage();
31
32    //Save the document
33    document.Save();
34}
35}

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


The result of running this code is appeared as

Add Image

You can download examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.