在 PostScript 中使用透明度 | C++

在 PS 文档中添加透明度

PostScript 不支持在绘制矢量图形对象时使用透明度。但是,半透明(部分透明)图像可以渲染为一组完全透明和完全不透明的像素。此类图像称为蒙版

Aspose.Page for C++ 库提供了一种将透明图像添加到 PS 文档的方法。对于绘制矢量图形(例如形状或文本),我们提供**“伪透明”**。

“伪透明” 是一种将 Alpha 值小于 255 的颜色变浅的过程。它是通过将红色、绿色和蓝色成分与 Alpha 值 1 进行特定混合来实现的。

“伪透明” 当然不允许我们从上层透明层下方看到下层彩色层,但如果底层为白色,则会造成透明的错觉。

在 PS 文档中添加透明图像

正如我们之前所写,透明图像可以作为蒙版添加到 PS 文档中,Aspose.Page for C++ 库为此提供了**AddTransparentImage()**方法。

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

在下面的示例中,我们演示了使用**AddImage()AddTransparentImage()**在 PS 文档中添加透明图像的区别。为了显示白色的半透明图像,我们将页面背景颜色设置为非白色。

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

  1. 为生成的 PS 文件创建输出流。
  2. 使用默认选项创建 PsSaveOptions 对象。如有需要,可更改背景颜色。
  3. 使用已创建的输出流和保存选项创建一个单页 PsDocument。
  4. 创建新的图形状态。
  5. 从图像文件创建 Bitmap
  6. 为图像创建必要的转换。
  7. 如果我们确定图像不透明,则将其作为完全不透明图像(使用 AddImage() 方法)添加到 PsDocument;如果我们不确定图像不透明,则将其作为透明图像(使用 AddTransparentImage() 方法)添加。
  8. 从当前图形状态退出到上一级图形状态。
  9. 关闭页面。
  10. 保存文档。
 1    // The path to the documents directory.
 2    System::String dataDir = RunExamples::GetDataDir_WorkingWithTransparency();
 3    
 4    //Create output stream for PostScript document
 5    {
 6        System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"AddTransparentImage_outPS.ps", System::IO::FileMode::Create);
 7        // Clearing resources under 'using' statement
 8        System::Details::DisposeGuard<1> __dispose_guard_2({ outPsStream});
 9        // ------------------------------------------
10        
11        try
12        {
13            //Create save options with A4 size
14            System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
15            //Set page's background color to see white image on it's own transparent background
16            options->set_BackgroundColor(System::Drawing::Color::FromArgb(211, 8, 48));
17            
18            // Create new 1-paged PS Document
19            System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, false);
20            
21            
22            document->WriteGraphicsSave();
23            document->Translate(20.0f, 100.0f);
24            
25            //Create bitmap from translucent image file
26            {
27                System::SharedPtr<System::Drawing::Bitmap> image = System::MakeObject<System::Drawing::Bitmap>(dataDir + u"mask1.png");
28                // Clearing resources under 'using' statement
29                System::Details::DisposeGuard<1> __dispose_guard_0({ image});
30                // ------------------------------------------
31                
32                try
33                {
34                    //Add this image to document as usual opaque RGB image
35                    document->DrawImage(image, System::MakeObject<System::Drawing::Drawing2D::Matrix>(1.0f, 0.0f, 0.0f, 1.0f, 100.0f, 0.0f), System::Drawing::Color::Empty);
36                }
37                catch(...)
38                {
39                    __dispose_guard_0.SetCurrentException(std::current_exception());
40                }
41            }
42            
43            //Again create bitmap from the same image file
44            {
45                System::SharedPtr<System::Drawing::Bitmap> image = System::MakeObject<System::Drawing::Bitmap>(dataDir + u"mask1.png");
46                // Clearing resources under 'using' statement
47                System::Details::DisposeGuard<1> __dispose_guard_1({ image});
48                // ------------------------------------------
49                
50                try
51                {
52                    //Add this image to document as transparent image image
53                    document->DrawTransparentImage(image, System::MakeObject<System::Drawing::Drawing2D::Matrix>(1.0f, 0.0f, 0.0f, 1.0f, 350.0f, 0.0f), 255);
54                }
55                catch(...)
56                {
57                    __dispose_guard_1.SetCurrentException(std::current_exception());
58                }
59            }
60            
61            document->WriteGraphicsRestore();
62            
63            //Close current page
64            document->ClosePage();
65            
66            //Save the document
67            document->Save();
68        }
69        catch(...)
70        {
71            __dispose_guard_2.SetCurrentException(std::current_exception());
72        }
73    }

请参阅 .NETJava 中 PS 文档透明度的处理。

运行此代码的结果如下:

添加透明图像

添加透明矢量图形对象

之前我们提到,Aspose.Page for C++ 库使用了一种用于透明形状和文本的变色算法,我们称之为**“伪透明”**。 在下面的示例中,我们演示了两个使用相同颜色绘制的形状之间的区别,但第一个形状没有 Alpha 分量,而第二个形状有 Alpha 分量。

 1    // The path to the documents directory.
 2    System::String dataDir = RunExamples::GetDataDir_WorkingWithTransparency();
 3    
 4    //Create output stream for PostScript document
 5    {
 6        System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"ShowPseudoTransparency_outPS.ps", System::IO::FileMode::Create);
 7        // Clearing resources under 'using' statement
 8        System::Details::DisposeGuard<1> __dispose_guard_0({ 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            float offsetX = 50.0f;
20            float offsetY = 100.0f;
21            float width = 200.0f;
22            float height = 100.0f;
23            
24            ///////////////////////////////// Create rectangle with opaque gradient fill /////////////////////////////////////////////////////////
25            System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
26            path->AddRectangle(System::Drawing::RectangleF(offsetX, offsetY, width, height));
27            
28            System::SharedPtr<System::Drawing::Drawing2D::LinearGradientBrush> opaqueBrush = System::MakeObject<System::Drawing::Drawing2D::LinearGradientBrush>(System::Drawing::RectangleF(0.0f, 0.0f, 200.0f, 100.0f), System::Drawing::Color::FromArgb(0, 0, 0), System::Drawing::Color::FromArgb(40, 128, 70), 0.f);
29            System::SharedPtr<System::Drawing::Drawing2D::Matrix> brushTransform = System::MakeObject<System::Drawing::Drawing2D::Matrix>(width, 0.0f, 0.0f, height, offsetX, offsetY);
30            opaqueBrush->set_Transform(brushTransform);
31            System::SharedPtr<GradientBrush> gradientBrush = System::MakeObject<GradientBrush>(opaqueBrush);
32            gradientBrush->set_WrapMode(System::Drawing::Drawing2D::WrapMode::Clamp);
33            
34            document->SetPaint(gradientBrush);
35            document->Fill(path);
36            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
37            
38            offsetX = 350.0f;
39            
40            ///////////////////////////////// Create rectangle with translucent gradient fill ///////////////////////////////////////////////////
41            //Create graphics path from the first rectangle
42            path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
43            path->AddRectangle(System::Drawing::RectangleF(offsetX, offsetY, width, height));
44            
45            //Create linear gradient brush colors which transparency are not 255, but 150 and 50. So it are translucent.
46            System::SharedPtr<System::Drawing::Drawing2D::LinearGradientBrush> translucentBrush = System::MakeObject<System::Drawing::Drawing2D::LinearGradientBrush>(System::Drawing::RectangleF(0.0f, 0.0f, width, height), System::Drawing::Color::FromArgb(150, 0, 0, 0), System::Drawing::Color::FromArgb(50, 40, 128, 70), 0.f);
47            //Create a transform for brush.
48            brushTransform = System::MakeObject<System::Drawing::Drawing2D::Matrix>(width, 0.0f, 0.0f, height, offsetX, offsetY);
49            //Set transform
50            translucentBrush->set_Transform(brushTransform);
51            //Create GradientBrush object containing the linear gradient brush
52            gradientBrush = System::MakeObject<GradientBrush>(translucentBrush);
53            gradientBrush->set_WrapMode(System::Drawing::Drawing2D::WrapMode::Clamp);
54            //Set paint
55            document->SetPaint(gradientBrush);
56            //Fill the rectangle
57            document->Fill(path);
58            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
59            
60            //Close current page
61            document->ClosePage();
62            
63            //Save the document
64            document->Save();
65        }
66        catch(...)
67        {
68            __dispose_guard_0.SetCurrentException(std::current_exception());
69        }
70    }

请参阅 .NETJava 中 PS 文档透明度的处理。

运行此代码的结果如下

显示伪透明

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.