图像处理 | C++ API 解决方案

在 XPS 文档中添加图像

添加图像

Aspose.Page for C++ API 允许您创建/读取 XPS 文档并向其中添加图像。 这可以通过创建 XpsMatrixXpsImageBrush 来实现,并将图像添加到 XPS 文件中。 要将图像添加到 XPS 文档,请按照以下步骤操作。

  1. 创建 XpsDocument 类的对象
  2. 创建 XpsPath 对象,并定义路径几何
  3. 使用 set_RenderTransform 设置路径的渲染变换
  4. 使用 set_Fill 使用创建的图像画笔填充路径
  5. 使用 Save 方法将文档保存到磁盘
 1For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
 2// Create new XPS Document
 3System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>();
 4// Add Image
 5System::SharedPtr<XpsPath> path = doc->AddPath(doc->CreatePathGeometry(u"M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
 6//Creating a matrix is optional, it can be used for proper positioning
 7path->set_RenderTransform(doc->CreateMatrix(0.7f, 0.f, 0.f, 0.7f, 0.f, 20.f));
 8//Create Image Brush
 9path->set_Fill(doc->CreateImageBrush(dataDir() + u"QL_logo_color.tif", System::Drawing::RectangleF(0.f, 0.f, 258.24f, 56.64f), System::Drawing::RectangleF(50.f, 20.f, 193.68f, 42.48f)));
10// Save resultant XPS document
11doc->Save(outDir() + u"AddImage_out.xps");

添加平铺图像

本解决方案提供了 XpsPath 类,您可以使用它在 XPS 文档中添加图像。 按照上述方法创建 Matrix 和 ImageBrush 后,指定 XpsTileMode.Tile 模式。

您可以将 ImageBrush 的 Opacity 值设置为 0 到 1。 然后保存文档。 以下代码片段展示了在 XPS 文档中添加平铺图像的完整功能:

 1For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
 2// Create new XPS Document
 3System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>();
 4// Tile image
 5// ImageBrush filled rectangle in the right top bellow
 6System::SharedPtr<XpsPath> path = doc->AddPath(doc->CreatePathGeometry(u"M 10,160 L 228,160 228,305 10,305"));
 7path->set_Fill(doc->CreateImageBrush(dataDir() + u"R08LN_NN.jpg", System::Drawing::RectangleF(0.f, 0.f, 128.f, 96.f), System::Drawing::RectangleF(0.f, 0.f, 64.f, 48.f)));
 8(System::DynamicCast<Aspose::Page::Xps::XpsModel::XpsImageBrush>(path->get_Fill()))->set_TileMode(Aspose::Page::Xps::XpsModel::XpsTileMode::Tile);
 9path->get_Fill()->set_Opacity(0.5f);
10// Save resultant XPS document
11doc->Save(outDir() + u"AddTiledImage_out.xps");

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.