裁剪并调整 EPS 大小

用于管理 EPS 图像大小的原生 C++ API 解决方案。

 

转换您的 EPS 图像变得前所未有的简单!使用 Aspose.Page for C++,您只需几行代码即可将 EPS 文件裁剪或修改为您确切规范的尺寸。想放大一点?没问题。需要小一点?我们满足您的需求。我们的 C++ API 允许您精确缩放图像,确保其完美符合您的愿景。但是,您为什么可能想要裁剪或调整 EPS 图像的大小呢?

  • 如果您正在为印刷品或数字文档设计版面,您可能需要调整 EPS 图像的大小以适应特定尺寸。
  • 通过缩小图像大小,您可以减小文件大小,使其更容易共享、上传或打印。较小的图像文件在网站和数字文档中加载得也更快,从而增强了用户体验。
  • 裁剪 (Cropping) 可以帮助消除图像中分散注意力的或不相关的部分,将注意力集中在关键元素上,并且它还可以更改图像的纵横比以更好地适​​应您的设计需求。因此,谨慎的裁剪可以改善图像的整体构图和视觉吸引力。
  • 裁剪和调整大小有助于针对 Web 用途优化图像,确保其快速加载并在不同大小的屏幕上呈现良好。

Aspose.Page 为您提供了轻松管理 EPS 文件边框的功能。使用此 API,您可以使用 C# 缩放或裁剪图像。要了解有关 如何处理 EPS 文件 的更多信息,请参阅文档。要查看该功能的在线版本,请尝试跨平台应用 EPS CropEPS Resize

要使用该功能,您首先需要获取解决方案:

  • 打开 NuGet 包管理器,搜索 Aspose.Page 并安装。您也可以在程序包管理器控制台 (Package Manager Console) 中使用以下命令。

Package Manager Console Command

    PM> Install-Package Aspose.Page

更改 EPS 图像大小的步骤。

下面的示例显示了使用所选单位(点 - points)调整 .eps 大小的过程。顺序如下:

  1. 使用包含 EPS 文件的输入流初始化 PsDocument 对象。
  2. 使用 ExtractEpsSize() 静态方法定义图像的现有大小。
  3. 为生成的 EPS 文件创建一个输出流。
  4. 使用 ResizeEps() 静态方法,按点为单位的全新大小调整 PsDocument 对象的大小。
  5. 保存调整大小后的 EPS 文件。
调整 EPS 大小
    // The path to the documents directory.
    System::String dataDir = RunExamples::GetDataDir_WorkingWithEPS();
    
    //Create an input stream for EPS file
    {
        System::SharedPtr<System::IO::Stream> inputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"input.eps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
        // Clearing resources under 'using' statement
        System::Details::DisposeGuard<1> __dispose_guard_1({ inputEpsStream});
        // ------------------------------------------
        
        try
        {
            //Initialize PsDocument object with input stream
            System::SharedPtr<PsDocument> doc = System::MakeObject<PsDocument>(inputEpsStream);
            
            //Get size of EPS image
            System::Drawing::Size oldSize = doc->ExtractEpsSize();
            
            //Create an output stream for resized EPS
            {
                System::SharedPtr<System::IO::Stream> outputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"output/" + u"output_resize_points.eps", System::IO::FileMode::Create, System::IO::FileAccess::Write);
                // Clearing resources under 'using' statement
                System::Details::DisposeGuard<1> __dispose_guard_0({ outputEpsStream});
                // ------------------------------------------
                
                try
                {
                    //Increase EPS size in 2 times and save to the output stream
                    doc->ResizeEps(outputEpsStream, System::Drawing::SizeF(static_cast<float>(oldSize.get_Width() * 2), static_cast<float>(oldSize.get_Height() * 2)), Aspose::Page::Units::Points);
                }
                catch(...)
                {
                    __dispose_guard_0.SetCurrentException(std::current_exception());
                }
            }
        }
        catch(...)
        {
            __dispose_guard_1.SetCurrentException(std::current_exception());
        }
    }

下面的示例显示了使用所选单位(点)进行裁剪而不是调整 .eps 大小的过程。顺序如下:

  1. 使用包含 EPS 文件的输入流初始化 PsDocument 对象。
  2. 使用 ExtractEpsBoundingBox() 静态方法定义图像现有的边界框 (bounding box)。
  3. 为生成的 EPS 文件创建一个输出流。
  4. 使用 CropEps() 静态方法使用新的边界框裁剪 PsDocument 对象。
  5. 保存裁剪后的 EPS 文件。
裁剪 EPS
    // The path to the documents directory.
    System::String dataDir = RunExamples::GetDataDir_WorkingWithEPS();
    
    //Create an input stream for EPS file
    {
        System::SharedPtr<System::IO::Stream> inputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"input.eps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
        // Clearing resources under 'using' statement
        System::Details::DisposeGuard<1> __dispose_guard_1({ inputEpsStream});
        // ------------------------------------------
        
        try
        {
            //Initialize PsDocument object with input stream
            System::SharedPtr<PsDocument> doc = System::MakeObject<PsDocument>(inputEpsStream);
            
            //Get initial bounding box of EPS image
            System::ArrayPtr<int32_t> initialBoundingBox = doc->ExtractEpsBoundingBox();
            
            //Create an output stream for resized EPS
            {
                System::SharedPtr<System::IO::Stream> outputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"output/" + u"output_crop.eps", System::IO::FileMode::Create, System::IO::FileAccess::Write);
                // Clearing resources under 'using' statement
                System::Details::DisposeGuard<1> __dispose_guard_0({ outputEpsStream});
                // ------------------------------------------
                
                try
                {
                    //Create new bounding box
                    //Bounding box is represented by 4 numbers: x0, y0, x, y, where x0 - left margin, y0 - top margin, x - (x0 + width), y - (y0 + height)
                    System::ArrayPtr<float> newBoundingBox = System::MakeArray<float>({260, 300, 480, 432});
                    
                    //Crop EPS image and save to the output stream                    
                    //Croping of image is changing of its bounding box so that new values of bounding box will be within initial bounding box, that is
                    //initialBoundingBox[0] <= newBoundingBox[0] <= initialBoundingBox[2]
                    //initialBoundingBox[1] <= newBoundingBox[1] <= initialBoundingBox[3]
                    //initialBoundingBox[0] <= newBoundingBox[2] <= initialBoundingBox[2]
                    //initialBoundingBox[1] <= newBoundingBox[3] <= initialBoundingBox[3]
                    doc->CropEps(outputEpsStream, newBoundingBox);
                }
                catch(...)
                {
                    __dispose_guard_0.SetCurrentException(std::current_exception());
                }
            }
        }
        catch(...)
        {
            __dispose_guard_1.SetCurrentException(std::current_exception());
        }
    }

EPS 什么是EPS文件格式

EPS(Encapsulated PostScript)是一种基于 PostScript 的单页描述格式,适用于矢量图形和矢量‑光栅混合图像。导入后文件不可编辑,建议转换为可编辑格式(如 SVG 或 PDF)后使用。