在 PostScript 中处理图像 | C++

Contents
[ Hide Show ]

在 PS 文档中添加图像

Aspose.Page for C++ 库提供了两种将图像添加到 PS 文档的方法:

之所以采用这种方法,是因为 PostScript 不支持透明度,而半透明图像可以渲染为一组完全透明和完全不透明的像素。这些图像被称为蒙版。如果我们想在 PS 文档中将半透明图像显示为蒙版,以更好地反映图像的透明度,我们需要对此类图像进行一些检查和预处理。

检查和预处理需要时间。因此,如果确定图像完全不透明,最好使用第一种方法,因为它可以节省执行时间。

第二种方法可以识别图像是完全不透明、完全透明还是半透明。如果图像完全不透明,则在第一种方法中将其添加为不透明图像; 如果图像完全透明,则根本不会添加到文档中;如果图像是半透明图像,则将其添加为 PostScript 图像蒙版

在下面的示例中,我们演示了如何添加完全不透明的图像。“使用透明度”文章中将演示如何添加透明图像。

在本例中,为了使用 Aspose.Page for C++ 库将图像添加到新的 PsDocument,我们执行以下步骤:

  1. 为生成的 PS 文件创建输出流。
  2. 使用默认选项创建 PsSaveOptions 对象。
  3. 使用已创建的输出流和保存选项创建一个单页 PsDocument。
  4. 创建新的图形状态。
  5. 从图像文件创建 System.Drawing.Bitmap
  6. 为图像创建必要的转换。
  7. 将图像添加到 PsDocument 对象。
  8. 从当前图形状态退出到上一级图形状态。
  9. 关闭页面。 11.保存文档。
 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    }

请参阅如何在 .NETJava 中处理 PS 文档中的图像。

运行此代码的结果显示为

添加图像

您可以从 GitHub 下载示例和数据文件。

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.