ครอบตัดและปรับขนาด EPS

โซลูชัน C++ API ดั้งเดิมสำหรับจัดการขนาดของภาพ EPS

 

การแปลงภาพ EPS ของคุณนั้นง่ายขึ้น! ด้วย Aspose.Page สำหรับ C++ คุณสามารถครอบตัดหรือเปลี่ยนขนาดของไฟล์ EPS ให้ตรงตามข้อกำหนดเฉพาะของคุณได้ในโค้ดเพียงไม่กี่บรรทัด ต้องการทำให้ใหญ่ขึ้นหรือไม่? ไม่มีปัญหา ต้องการทำให้เล็กลงหรือไม่? เราจัดการให้คุณได้ API ของเราสำหรับ C++ ช่วยให้คุณสามารถปรับขนาดภาพได้อย่างแม่นยำ เพื่อให้แน่ใจว่าเข้ากับวิสัยทัศน์ของคุณได้อย่างลงตัว แต่ทำไมคุณถึงต้องการครอบตัดหรือปรับขนาดรูปภาพ EPS

  • หากคุณกำลังออกแบบเค้าโครงสำหรับสิ่งพิมพ์หรือเอกสารดิจิทัล คุณอาจต้องปรับขนาดภาพ EPS เพื่อให้พอดีกับขนาดที่กำหนด
  • ด้วยการลดขนาดภาพ คุณสามารถลดขนาดไฟล์ ทำให้ง่ายต่อการแชร์ อัปโหลด หรือพิมพ์ ไฟล์ภาพที่เล็กลงยังโหลดเร็วขึ้นบนเว็บไซต์และในเอกสารดิจิทัล ซึ่งจะปรับปรุงประสบการณ์ของผู้ใช้
  • การครอบตัด (Cropping) สามารถช่วยกำจัดส่วนที่น่ารำคาญหรือไม่เกี่ยวข้องของรูปภาพ เน้นความสนใจไปที่องค์ประกอบหลัก และยังสามารถเปลี่ยนอัตราส่วนกว้างยาวของรูปภาพเพื่อให้เหมาะกับความต้องการในการออกแบบของคุณได้ดียิ่งขึ้น ดังนั้นการครอบตัดอย่างระมัดระวังสามารถปรับปรุงองค์ประกอบโดยรวมและความน่าดึงดูดใจของรูปภาพได้
  • การครอบตัดและการปรับขนาดสามารถช่วยปรับภาพให้เหมาะสมสำหรับการใช้งานบนเว็บ ทำให้มั่นใจได้ว่าโหลดได้รวดเร็วและดูดีบนหน้าจอขนาดต่างๆ กัน

Aspose.Page มอบฟังก์ชันการทำงานเพื่อจัดการขอบเขตของไฟล์ EPS ได้อย่างง่ายดาย ด้วย API นี้ คุณสามารถวัดสัดส่วนภาพหรือครอบตัดภาพโดยใช้ C# เรียนรู้เพิ่มเติมเกี่ยวกับ วิธีจัดการกับไฟล์ EPS ให้ทำตามเอกสารประกอบ ดูเวอร์ชันใช้งานจริงของฟังก์ชันการทำงาน ลองใช้แอปข้ามแพลตฟอร์ม EPS Crop และ EPS Resize

ในการใช้ฟังก์ชันการทำงานนี้ ก่อนอื่นคุณต้องรับโซลูชัน:

  • เปิดโปรแกรมจัดการแพ็กเกจ NuGet แล้วค้นหา Aspose.Page แล้วติดตั้ง คุณอาจใช้คำสั่งต่อไปนี้ จาก Package Manager Console

Package Manager Console Command

    PM> Install-Package Aspose.Page

ขั้นตอนในการเปลี่ยนขนาดของภาพ EPS

ตัวอย่างด้านล่างแสดงขั้นตอนการปรับขนาด .eps โดยใช้หน่วยที่เลือก - พอยต์ (points) ลำดับคือ:

  1. เริ่มต้นอ็อบเจ็กต์ PsDocument ด้วยอินพุตสตรีม (input stream) ที่มีไฟล์ EPS
  2. กำหนดขนาดที่มีอยู่ของอิมเมจโดยใช้เมธอดสถิต ExtractEpsSize()
  3. สร้างเอาต์พุตสตรีม (output stream) สำหรับไฟล์ EPS ผลลัพธ์
  4. ปรับขนาดวัตถุ PsDocument เป็นขนาดใหม่เป็นจุด (พอยต์) ด้วยวิธีการคงที่ ResizeEps()
  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 ด้วยหน่วยที่เลือก - พอยต์ (points) ลำดับคือ:

  1. เริ่มต้นวัตถุ PsDocument โดยมีอินพุตสตรีมที่มีไฟล์ EPS
  2. กำหนดกรอบการล้อมกรอบที่รัดรูป (bounding box) ที่มีอยู่ของรูปภาพโดยใช้เมธอดสถิต ExtractEpsBoundingBox()
  3. สร้างเอาต์พุตสตรีมสำหรับไฟล์ EPS ผลลัพธ์
  4. ครอบตัดอ็อบเจ็กต์ PsDocument ด้วยกรอบสี่เหลี่ยมรอบ (bounding box) ใหม่ด้วยวิธีสถิต CropEps()
  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 What is EPS File Format

EPS (Encapsulated PostScript) เป็นรูปแบบที่อธิบายหน้าหนึ่งหน้าโดยใช้ PostScript เหมาะกับกราฟิกเวกเตอร์และเวกเตอร์‑แรสเตอร์ผสม หลังจากนำเข้าไฟล์จะไม่สามารถแก้ไขได้ จึงแนะนำให้แปลงเป็นรูปแบบที่แก้ไขได้ เช่น SVG หรือ PDF