调整 EPS 大小 | C++ API 解决方案
概述
本文介绍如何使用 C++ 调整 EPS 文件大小。内容涵盖以下主题:
C++ 调整 EPS 文件大小说明
调整图像大小是指更改图像的一个或两个尺寸(宽度和高度)的操作。图像内容保持不变,但图像本身可以根据新的宽度和高度值进行缩放。如果宽度和高度按比例增加,EPS 图像的显示效果会放大,否则会缩小。如果宽度和高度的变化不成比例,则 EPS 图像的显示效果会在某些方面被压缩或拉长。由于我们的解决方案并非针对内容,而是针对 EPS 文件的标题和设置部分,因此 EPS 文件的体积几乎保持不变。 要设置 EPS 图像显示效果的新尺寸,通常需要了解其现有尺寸并选择用于指定新尺寸的单位。单位可以是磅(1/72 英寸)、英寸、毫米、厘米和百分比。 因此,在 C++ 中调整 EPS 图像大小的步骤如下:
- 使用包含 EPS 文件的输入流初始化 PsDocument 对象。
- 使用静态方法 ExtractEpsSize 提取图像的现有大小。
- 为生成的 EPS 文件创建输出流。
- 使用静态方法 ResizeEps,以选定的 单位 调整 PsDocument 对象的大小,使其达到新的大小。
您可以通过免费在线 Resize EPS 检查 Aspose.Page EPS Resize 的质量并查看结果,然后使用我们的 EPS Viewer 查看生成的 EPS 文件
在 C++ 中,使用 Points 设置 EPS Resize 的新尺寸
在以下 C++ 代码片段中,图像的新尺寸由 Points(1/72 英寸)设置:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
2
3 // The path to the documents directory.
4 System::String dataDir = RunExamples::GetDataDir_WorkingWithEPS();
5
6 //Create an input stream for EPS file
7 {
8 System::SharedPtr<System::IO::Stream> inputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"input.eps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
9 // Clearing resources under 'using' statement
10 System::Details::DisposeGuard<1> __dispose_guard_1({ inputEpsStream});
11 // ------------------------------------------
12
13 try
14 {
15 //Initialize PsDocument object with input stream
16 System::SharedPtr<PsDocument> doc = System::MakeObject<PsDocument>(inputEpsStream);
17
18 //Get size of EPS image
19 System::Drawing::Size oldSize = doc->ExtractEpsSize();
20
21 //Create an output stream for resized EPS
22 {
23 System::SharedPtr<System::IO::Stream> outputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"output_resize_points.eps", System::IO::FileMode::Create, System::IO::FileAccess::Write);
24 // Clearing resources under 'using' statement
25 System::Details::DisposeGuard<1> __dispose_guard_0({ outputEpsStream});
26 // ------------------------------------------
27
28 try
29 {
30 //Increase EPS size in 2 times and save to the output stream
31 doc->ResizeEps(outputEpsStream, System::Drawing::SizeF(static_cast<float>(oldSize.get_Width() * 2), static_cast<float>(oldSize.get_Height() * 2)), Aspose::Page::Units::Points);
32 }
33 catch(...)
34 {
35 __dispose_guard_0.SetCurrentException(std::current_exception());
36 }
37 }
38 }
39 catch(...)
40 {
41 __dispose_guard_1.SetCurrentException(std::current_exception());
42 }
43 }
对于 Linux、MacOS 和其他非 Windows 操作系统,我们建议使用我们的 Aspose.Page.Drawing Nuget 软件包。它使用 Aspose.Drawing 后端,而不是 System.Drawing 系统库。 因此,请导入 Aspose.Page.Drawing 命名空间,而不是 System.Drawing 命名空间。在以上和以下代码片段中,将使用 Aspose.Page.Drawing.Size 代替 System.Drawing.Size。我们在 GitHub 上的代码示例包含所有必要的替换。
使用 C++ 调整 EPS 大小,以英寸为单位设置新尺寸
在以下 C++ 代码片段中,图像的新尺寸以英寸为单位设置:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
2
3 // The path to the documents directory.
4 System::String dataDir = RunExamples::GetDataDir_WorkingWithEPS();
5
6 //Create an input stream for EPS file
7 {
8 System::SharedPtr<System::IO::Stream> inputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"input.eps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
9 // Clearing resources under 'using' statement
10 System::Details::DisposeGuard<1> __dispose_guard_1({ inputEpsStream});
11 // ------------------------------------------
12
13 try
14 {
15 //Initialize PsDocument object with input stream
16 System::SharedPtr<PsDocument> doc = System::MakeObject<PsDocument>(inputEpsStream);
17
18 //Get size of EPS image
19 System::Drawing::Size oldSize = doc->ExtractEpsSize();
20
21 //Create an output stream for resized EPS
22 {
23 System::SharedPtr<System::IO::Stream> outputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"output_resize_inches.eps", System::IO::FileMode::Create, System::IO::FileAccess::Write);
24 // Clearing resources under 'using' statement
25 System::Details::DisposeGuard<1> __dispose_guard_0({ outputEpsStream});
26 // ------------------------------------------
27
28 try
29 {
30 //Save EPS to the output stream with new size assigned in inches
31 doc->ResizeEps(outputEpsStream, System::Drawing::SizeF(5.791f, 3.625f), Aspose::Page::Units::Inches);
32 }
33 catch(...)
34 {
35 __dispose_guard_0.SetCurrentException(std::current_exception());
36 }
37 }
38 }
39 catch(...)
40 {
41 __dispose_guard_1.SetCurrentException(std::current_exception());
42 }
43 }
在 C++ 中调整 EPS 图像尺寸,以毫米为单位
以下 C++ 代码片段中,图像的新尺寸以毫米为单位设置:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
2
3 // The path to the documents directory.
4 System::String dataDir = RunExamples::GetDataDir_WorkingWithEPS();
5
6 //Create an input stream for EPS file
7 {
8 System::SharedPtr<System::IO::Stream> inputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"input.eps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
9 // Clearing resources under 'using' statement
10 System::Details::DisposeGuard<1> __dispose_guard_1({ inputEpsStream});
11 // ------------------------------------------
12
13 try
14 {
15 //Initialize PsDocument object with input stream
16 System::SharedPtr<PsDocument> doc = System::MakeObject<PsDocument>(inputEpsStream);
17
18 //Get size of EPS image
19 System::Drawing::Size oldSize = doc->ExtractEpsSize();
20
21 //Create an output stream for resized EPS
22 {
23 System::SharedPtr<System::IO::Stream> outputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"output_resize_mms.eps", System::IO::FileMode::Create, System::IO::FileAccess::Write);
24 // Clearing resources under 'using' statement
25 System::Details::DisposeGuard<1> __dispose_guard_0({ outputEpsStream});
26 // ------------------------------------------
27
28 try
29 {
30 //Save EPS to the output stream with new size assigned in millimeters
31 doc->ResizeEps(outputEpsStream, System::Drawing::SizeF(196.0f, 123.0f), Aspose::Page::Units::Millimeters);
32 }
33 catch(...)
34 {
35 __dispose_guard_0.SetCurrentException(std::current_exception());
36 }
37 }
38 }
39 catch(...)
40 {
41 __dispose_guard_1.SetCurrentException(std::current_exception());
42 }
43 }
在 C++ 中通过 Percents 设置 EPS 图像的新尺寸
以下 C++ 代码片段中,图像的新尺寸由 Percents 设置:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-C
2
3 // The path to the documents directory.
4 System::String dataDir = RunExamples::GetDataDir_WorkingWithEPS();
5
6 //Create an input stream for EPS file
7 {
8 System::SharedPtr<System::IO::Stream> inputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"input.eps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
9 // Clearing resources under 'using' statement
10 System::Details::DisposeGuard<1> __dispose_guard_1({ inputEpsStream});
11 // ------------------------------------------
12
13 try
14 {
15 //Initialize PsDocument object with input stream
16 System::SharedPtr<PsDocument> doc = System::MakeObject<PsDocument>(inputEpsStream);
17
18 //Get size of EPS image
19 System::Drawing::Size oldSize = doc->ExtractEpsSize();
20
21 //Create an output stream for resized EPS
22 {
23 System::SharedPtr<System::IO::Stream> outputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"output_resize_percents.eps", System::IO::FileMode::Create, System::IO::FileAccess::Write);
24 // Clearing resources under 'using' statement
25 System::Details::DisposeGuard<1> __dispose_guard_0({ outputEpsStream});
26 // ------------------------------------------
27
28 try
29 {
30 //Save EPS to the output stream with new size assigned in percents
31 doc->ResizeEps(outputEpsStream, System::Drawing::SizeF(200.0f, 200.0f), Aspose::Page::Units::Percents);
32 }
33 catch(...)
34 {
35 __dispose_guard_0.SetCurrentException(std::current_exception());
36 }
37 }
38 }
39 catch(...)
40 {
41 __dispose_guard_1.SetCurrentException(std::current_exception());
42 }
43 }
初始图像
调整大小的图像
在我们的调整 EPS 大小 Web 应用程序上在线评估调整 EPS 大小。您可以在几秒钟内调整 EPS 文件大小并下载结果。
您可以从
GitHub 下载示例和数据文件。