在 PostScript 中使用透明度 | .NET

在 PS 文档中添加透明度

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

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

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

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

在 PS 文档中添加透明图像

正如我们之前所写,透明图像可以作为蒙版添加到 PS 文档中,Aspose.Page for .NET 库为此提供了AddTransparentImage()方法。 此方法可以识别图像是完全不透明、完全透明还是半透明。如果是完全不透明,则会在AddImage()方法中将其作为不透明图像添加;如果是完全透明,则根本不会添加到文档中;如果是半透明图像,则会作为 PostScript 图像蒙版添加。

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

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

  1. 为生成的 PS 文件创建输出流。
  2. 使用默认选项创建 PsSaveOptions 对象。如有需要,可更改背景颜色。
  3. 使用已创建的输出流和保存选项创建一个单页 PsDocument。
  4. 创建新的图形状态。
  5. 从图像文件创建位图
  6. 为图像创建必要的转换。
  7. 如果我们确定图像不透明,则将其作为完全不透明图像(使用 AddImage() 方法)添加到 PsDocument;如果我们不确定图像不透明,则将其作为透明图像(使用 AddTransparentImage() 方法)添加。
  8. 从当前图形状态退出到上一级图形状态。
  9. 关闭页面。
  10. 保存文档。
 1//Create an output stream for PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "AddTransparentImage_outPS.ps", FileMode.Create))
 3{
 4    //Create save options with A4 size
 5    PsSaveOptions options = new PsSaveOptions();
 6    //Set page's background color to see white image on it's own transparent background
 7    options.BackgroundColor = Color.FromArgb(211, 8, 48);
 8
 9    // Create new 1-paged PS Document
10    PsDocument document = new PsDocument(outPsStream, options, false);
11
12
13    document.WriteGraphicsSave();
14    document.Translate(20, 100);
15
16    //Create a bitmap from translucent image file
17    using (Bitmap image = new Bitmap(dataDir + "mask1.png"))
18    {
19        //Add this image to the document as usual opaque RGB image
20        document.DrawImage(image, new Matrix(1, 0, 0, 1, 100, 0), Color.Empty);
21    }
22
23    //Again create a bitmap from the same image file
24    using (Bitmap image = new Bitmap(dataDir + "mask1.png"))
25    {
26        //Add this image to the document as transparent image
27        document.DrawTransparentImage(image, new Matrix(1, 0, 0, 1, 350, 0), 255);
28    }
29
30    document.WriteGraphicsRestore();
31
32    //Close current page
33    document.ClosePage();
34
35    //Save the document
36    document.Save();
37}

对于 Linux、MacOS 和其他非 Windows 操作系统,我们建议使用我们的 Aspose.Page.Drawing Nuget 软件包。它使用 Aspose.Drawing 后端,而非 System.Drawing 系统库。

因此,请导入 Aspose.Page.Drawing 命名空间,而不是 System.Drawing 命名空间。在以上和以下代码片段中,将使用 Aspose.Page.Drawing.Bitmap 代替 System.Drawing.Bitmap,使用 Aspose.Page.Drawing.Drawing2D.Matrix 代替 System.Drawing.Drawing2D.Matrix,等等。我们在 GitHub 上的代码示例包含所有必要的替换。

请参阅 Java 中关于 PS 文档透明度的说明。

运行此代码的结果如下:

添加透明图像

添加透明矢量图形对象

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

 1//Create an output stream for PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "ShowPseudoTransparency_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    float offsetX = 50;
11    float offsetY = 100;
12    float width = 200;
13    float height = 100;
14
15///////////////////////////////// Create a rectangle with opaque gradient fill /////////////////////////////////////////////////////////
16    GraphicsPath path = new GraphicsPath();
17    path.AddRectangle(new RectangleF(50, 100, 200, 100));
18
19    LinearGradientBrush opaqueBrush = new LinearGradientBrush(new RectangleF(0, 0, 200, 100), Color.FromArgb(0, 0, 0),
20        Color.FromArgb(40, 128, 70), 0f);
21    Matrix brushTransform = new Matrix(200, 0, 0, 100, 50, 100);
22    opaqueBrush.Transform = brushTransform;
23    Aspose.Page.EPS.GradientBrush gradientBrush = new GradientBrush(opaqueBrush);
24    gradientBrush.WrapMode = WrapMode.Clamp;
25
26    document.SetPaint(gradientBrush);
27    document.Fill(path);
28/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
29
30    offsetX = 350;
31
32///////////////////////////////// Create a rectangle with translucent gradient fill ///////////////////////////////////////////////////
33    //Create graphics path from the first rectangle
34    path = new GraphicsPath();
35    path.AddRectangle(new RectangleF(offsetX, offsetY, width, height));
36
37    //Create linear gradient brush colors which transparency are not 255, but 150 and 50. So it are translucent.
38    LinearGradientBrush translucentBrush = new LinearGradientBrush(new RectangleF(0, 0, width, height), Color.FromArgb(150, 0, 0, 0),
39        Color.FromArgb(50, 40, 128, 70), 0f);
40    //Create a transform for the brush.
41    brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
42    //Set the transform
43    translucentBrush.Transform = brushTransform;
44    
45    //Set the paint
46    document.SetPaint(translucentBrush);
47    //Fill the rectangle
48    document.Fill(path);
49/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
50
51    //Close current page
52    document.ClosePage();
53
54    //Save the document
55    document.Save();
56}

请参阅 JavaC++ 中关于 PS 文档透明度的用法。

运行此代码的结果显示为

显示伪透明

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.