在 PostScript 中使用纹理 | C++
在 PS 文档中添加纹理平铺图案
纹理平铺图案是用于填充或绘制对象(例如形状或文本)的图像。如果图像尺寸小于对象尺寸,则会在 X 和 Y 方向上重复,以覆盖所有必要的区域。
在图形对象内重复图像的过程称为平铺。为了在 PsDocument 中设置绘制或描边,我们必须将 System.Drawing.Brush 类的对象(用于绘制)和 System.Drawing.Pen 类的对象(用于描边)分别传递给相应的方法。
Aspose.Page for C++ 库处理 C++ 平台提供的所有 System.Drawing.Brush 子类。这些画笔分别是 System.Drawing.SolidBrush、System.Drawing.TextureBrush、System.Drawing.LinearGradientBrush、System.Drawing.PathGradientBrush 和 System.Drawing.HatchBrush。System.Drawing.Pen 类由于是密封类,因此无法扩展,但它包含 System.Drawing.Brush 属性,因此 Aspose.Page for C++ 库也可以使用一整套画笔来绘制线条、勾勒形状和文本的轮廓。
为了在 Aspose.Page for C++ 库中用纹理图案绘制图形对象,只需将 System.Drawing.TextureBrush 传递给 SetPaint() 或 FillText() 或 FillAndStrokeText() 方法(这些方法接受 System.Drawing.Brush 作为参数)即可。
为了在 Aspose.Page for C++ 库中使用纹理图案勾勒图形对象,您需要创建一个新的 System.Drawing.Pen 对象和 System.Drawing.TextureBrush 对象,并将其传递给 SetStroke() 函数,或者将 OutlineText() 或 FillAndStrokeText() 方法(这些方法接受 System.Drawing.Pen 作为参数)之一。
在下面的示例中,我们演示了如何使用纹理平铺图案填充形状和文本,以及如何勾勒文本的轮廓。
示例中使用纹理图案和 PsDocument 的步骤描述如下:
- 为生成的 PS 文件创建输出流。
- 使用默认选项创建 PsSaveOptions 对象。
- 创建一个单页 PsDocument,并配置已创建的输出流和保存选项。
- 创建一个新的图形状态并将其平移到所需位置。
- 从图像文件创建 System.Drawing.Bitmap。
- 从图像创建 System.Drawing.TextureBrush。
- 在纹理画笔中设置必要的变换。
- 在 PsDocument 的当前图形状态中,将纹理画笔设置为当前绘制。
- 创建一个矩形路径。
- 使用纹理画笔填充矩形。
- 将当前绘制保存为局部变量以备将来使用。
- 使用红色画笔设置当前描边。
- 使用当前画笔勾勒矩形的轮廓。
- 从当前图形状态退出到上一级图形状态。
- 创建系统字体。
- 填充并描边(勾勒)文本。填充使用纹理画笔,描边使用黑色钢笔。
- 使用新建的System.Drawing.Pen,并以纹理画笔作为画笔,在另一个位置勾勒文本轮廓。
- 关闭页面。
- 保存文档。
1 // The path to the documents directory.
2 System::String dataDir = RunExamples::GetDataDir_WorkingWithTextures();
3
4 //Create output stream for PostScript document
5 {
6 System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"AddTextureTilingPattern_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(200.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"TestTexture.bmp");
26 // Clearing resources under 'using' statement
27 System::Details::DisposeGuard<1> __dispose_guard_0({ image});
28 // ------------------------------------------
29
30 try
31 {
32 //Create texture brush from the image
33 System::SharedPtr<System::Drawing::TextureBrush> brush = System::MakeObject<System::Drawing::TextureBrush>(image, System::Drawing::Drawing2D::WrapMode::Tile);
34
35 //Add scaling in X direction to the mattern
36 System::SharedPtr<System::Drawing::Drawing2D::Matrix> transform = System::MakeObject<System::Drawing::Drawing2D::Matrix>(2.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
37 brush->set_Transform(transform);
38
39 //Set this texture brush as current paint
40 document->SetPaint(brush);
41 }
42 catch(...)
43 {
44 __dispose_guard_0.SetCurrentException(std::current_exception());
45 }
46 }
47
48 //Create rectangle path
49 System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
50 path->AddRectangle(System::Drawing::RectangleF(0.0f, 0.0f, 200.0f, 100.0f));
51
52 //Fill rectangle
53 document->Fill(path);
54
55 //Get current paint
56 System::SharedPtr<System::Drawing::Brush> paint = document->GetPaint();
57
58 //Set red stroke
59 document->SetStroke(System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Red()), 2.0f));
60 //Stroke the rectangle
61 document->Draw(path);
62
63 document->WriteGraphicsRestore();
64
65 //Fill text with texture pattern
66 System::SharedPtr<System::Drawing::Font> font = System::MakeObject<System::Drawing::Font>(u"Arial", 96.0f, System::Drawing::FontStyle::Bold);
67 document->FillAndStrokeText(u"ABC", font, 200.0f, 300.0f, paint, System::MakeObject<System::Drawing::Pen>(System::Drawing::Color::get_Black(), 2.0f));
68
69 //Outline text with texture pattern
70 document->OutlineText(u"ABC", font, 200.0f, 400.0f, System::MakeObject<System::Drawing::Pen>(paint, 5.0f));
71
72 //Close current page
73 document->ClosePage();
74
75 //Save the document
76 document->Save();
77 }
78 catch(...)
79 {
80 __dispose_guard_1.SetCurrentException(std::current_exception());
81 }
82 }
运行此代码的结果如下
您可以从 GitHub下载示例和数据文件。