在 PostScript 中处理图像 | .NET

Contents
[ Hide Show ]

在 PS 文档中添加图像

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

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

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

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

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

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

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

对于 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 文档中的图像处理。

运行此代码的结果显示为:

添加图片

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.