Working with Images in PostScript | C++

Contents
[ Hide Show ]

Add Image in PS Document

Aspose.Page for C++ 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 C++ 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    // The path to the documents directory.
 2    System::String dataDir = RunExamples::GetDataDir_WorkingWithImages();
 3    
 4    //Create output stream for PostScript document
 5    {
 6        System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"AddImage_outPS.ps", System::IO::FileMode::Create);
 7        // Clearing resources under 'using' statement
 8        System::Details::DisposeGuard<1> __dispose_guard_1({ outPsStream});
 9        // ------------------------------------------
10        
11        try
12        {
13            //Create save options with A4 size
14            System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
15            
16            // Create new 1-paged PS Document
17            System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, false);
18            
19            
20            document->WriteGraphicsSave();
21            document->Translate(100.0f, 100.0f);
22            
23            //Create a Bitmap object from image file
24            {
25                System::SharedPtr<System::Drawing::Bitmap> image = System::MakeObject<System::Drawing::Bitmap>(dataDir + u"TestImage Format24bppRgb.jpg");
26                // Clearing resources under 'using' statement
27                System::Details::DisposeGuard<1> __dispose_guard_0({ image});
28                // ------------------------------------------
29                
30                try
31                {
32                    //Create image transform
33                    System::SharedPtr<System::Drawing::Drawing2D::Matrix> transform = System::MakeObject<System::Drawing::Drawing2D::Matrix>();
34                    transform->Translate(35.0f, 300.0f);
35                    transform->Scale(3.0f, 3.0f);
36                    transform->Rotate(-45.0f);
37                    
38                    //Add image to document
39                    document->DrawImage(image, transform, System::Drawing::Color::Empty);
40                }
41                catch(...)
42                {
43                    __dispose_guard_0.SetCurrentException(std::current_exception());
44                }
45            }
46            
47            document->WriteGraphicsRestore();
48            
49            //Close current page
50            document->ClosePage();
51            
52            //Save the document
53            document->Save();
54        }
55        catch(...)
56        {
57            __dispose_guard_1.SetCurrentException(std::current_exception());
58        }
59    }

See working with images in PS documents in .NET or Java.

The result of running this code is appeared as

Add Image

You can download examples and data files from GitHub.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.