使用 C++ 在簡報中管理圖片框
簡介
圖片框是一種包含圖像的形狀─它就像框中的圖片。
您可以透過圖片框將圖像新增到投影片中。這樣,您可以藉由格式化圖片框來格式化圖像。
建立圖片框
- 建立 Presentation 類別 的實例。
- 透過索引取得投影片的參考。
- 透過將影像加入與簡報物件關聯的 IImagescollection 以建立 IPPImage 物件,該物件將用於填充形狀。
- 指定影像的寬度與高度。
- 透過參照投影片的形狀物件所提供的
AddPictureFrame方法,根據影像的寬度與高度建立 PictureFrame。 - 將圖片框(包含圖片)加入投影片。
- 將已修改的簡報寫入為 PPTX 檔案。
此 C++ 程式碼展示如何建立圖片框:
// 文件目錄的路徑。
const String outPath = u"../out/PictureFrameFormatting_out.pptx";
const String filePath = u"../templates/Tulips.jpg";
// 載入所需的簡報
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// 取得第一張投影片
SharedPtr<ISlide> slide = pres->get_Slide(0);
// 載入將加入簡報影像集合的圖像
// 取得圖片
auto image = Images::FromFile(filePath);
// 將影像加入簡報的影像集合
SharedPtr<IPPImage> imgx = pres->get_Images()->AddImage(image);
// 將圖片框加入投影片
SharedPtr<IPictureFrame> pf = slide->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, 50, 50, 100, 100, imgx);
// 設定相對縮放的寬度與高度
pf->set_RelativeScaleHeight(0.8);
pf->set_RelativeScaleWidth(1.35);
// 對 PictureFrame 套用一些格式設定
pf->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
pf->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Blue());
pf->get_LineFormat()->set_Width ( 20);
pf->set_Rotation( 45);
//將 PPTX 檔案寫入磁碟
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);
建立具有相對縮放的圖片框
透過調整影像的相對縮放,您可以建立更為複雜的圖片框。
- 建立 Presentation 類別 的實例。
- 透過索引取得投影片的參考。
- 將影像加入簡報的影像集合。
- 透過將影像加入與簡報物件關聯的 IImagescollection 以建立 IPPImage 物件,該物件將用於填充形狀。
- 在圖片框中指定影像的相對寬度與高度。
- 將已修改的簡報寫入為 PPTX 檔案。
此 C++ 程式碼展示如何建立具有相對縮放的圖片框:
// 文件目錄的路徑。
const String outPath = u"../out/AddRelativeScaleHeightPictureFrame_out.pptx";
const String filePath = u"../templates/Tulips.jpg";
// 載入所需的簡報
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// 取得第一張投影片
SharedPtr<ISlide> slide = pres->get_Slide(0);
// 載入要加入簡報影像集合的圖像
// 取得圖片
auto image = Images::FromFile(filePath);
// 將影像加入簡報的影像集合
SharedPtr<IPPImage> imgx = pres->get_Images()->AddImage(image);
// 將圖片框加入投影片
SharedPtr<IPictureFrame> pf = slide->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, 50, 50, 100, 100, imgx);
// 設定相對縮放的寬度與高度
pf->set_RelativeScaleHeight (0.8);
pf->set_RelativeScaleWidth(1.35);
//將 PPTX 檔案寫入磁碟
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);
從圖片框擷取點陣圖影像
您可以從 PictureFrame 物件擷取點陣圖影像,並以 PNG、JPG 等格式儲存。以下程式碼示範如何從文件「sample.pptx」中擷取影像並以 PNG 格式儲存。
auto presentation = MakeObject<Presentation>(u"sample.pptx");
auto firstSlide = presentation->get_Slide(0);
auto firstShape = firstSlide->get_Shape(0);
if (ObjectExt::Is<IPictureFrame>(firstShape))
{
auto pictureFrame = ExplicitCast<IPictureFrame>(firstShape);
auto image = pictureFrame->get_PictureFormat()->get_Picture()->get_Image()->get_SystemImage();
image->Save(u"slide_1_shape_1.png", ImageFormat::get_Png());
}
presentation->Dispose();
從圖片框擷取 SVG 影像
當簡報在 PictureFrame 形狀中包含 SVG 圖形時,Aspose.Slides for C++ 可讓您以完整保真度取得原始向量影像。透過遍歷投影片的形狀集合,您可以辨識每個 PictureFrame,檢查底層的 IPPImage 是否包含 SVG 內容,然後將該影像以原生 SVG 格式儲存至磁碟或串流。
以下程式碼示範如何從圖片框擷取 SVG 影像:
auto presentation = MakeObject<Presentation>(u"sample.pptx");
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shape(0);
if (ObjectExt::Is<IPictureFrame>(shape))
{
auto pictureFrame = ExplicitCast<IPictureFrame>(shape);
auto svgImage = pictureFrame->get_PictureFormat()->get_Picture()->get_Image()->get_SvgImage();
if (svgImage != nullptr)
{
File::WriteAllText(u"output.svg", svgImage->get_SvgContent());
}
}
presentation->Dispose();
取得影像的透明度
Aspose.Slides 允許您取得套用於影像的透明度效果。此 C++ 程式碼示範此操作:
auto presentation = System::MakeObject<Presentation>(u"Test.pptx");
auto pictureFrame = System::ExplicitCast<IPictureFrame>(presentation->get_Slide(0)->get_Shape(0));
auto imageTransform = pictureFrame->get_PictureFormat()->get_Picture()->get_ImageTransform();
for (auto&& effect : imageTransform)
{
if (System::ObjectExt::Is<IAlphaModulateFixed>(effect))
{
float transparencyValue = 100.0f - (System::ExplicitCast<IAlphaModulateFixed>(effect))->get_Amount();
System::Console::WriteLine(System::String(u"Picture transparency: ") + transparencyValue);
}
}
取得影像的亮度與對比度
Aspose.Slides 允許您取得套用於影像的亮度與對比度效果。ILuminance 介面代表此影像變換效果。
此 C++ 程式碼示範如何從圖片框取得亮度與對比度設定:
auto presentation = System::MakeObject<Presentation>(u"sample.pptx");
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shape(0);
auto pictureFrame = System::ExplicitCast<IPictureFrame>(shape);
auto imageTransform = pictureFrame->get_PictureFormat()->get_Picture()->get_ImageTransform();
for (auto&& effect : imageTransform)
{
if (System::ObjectExt::Is<ILuminance>(effect))
{
auto luminance = System::ExplicitCast<ILuminance>(effect)->GetEffective();
auto brightness = luminance->get_Brightness();
auto contrast = luminance->get_Contrast();
Console::WriteLine(System::String(u"Brightness: ") + brightness);
Console::WriteLine(System::String(u"Contrast: ") + contrast);
}
}
presentation->Dispose();
圖片框格式設定
Aspose.Slides 提供許多可套用於圖片框的格式化選項。使用這些選項,您可以調整圖片框以符合特定需求。
- 建立 Presentation 類別 的實例。
- 透過索引取得投影片的參考。
- 透過將影像加入與簡報物件關聯的 IImagescollection 以建立 IPPImage 物件,該物件將用於填充形狀。
- 指定影像的寬度與高度。
- 透過參照投影片的 IShapes 物件所提供的 AddPictureFrame 方法,根據影像的寬度與高度建立
PictureFrame。 - 將圖片框(包含圖片)加入投影片。
- 設定圖片框的線條顏色。
- 設定圖片框的線條寬度。
- 以正值或負值旋轉圖片框
- 正值會順時針旋轉影像。
- 負值會逆時針旋轉影像。
- 再次將圖片框(包含圖片)加入投影片。
- 將已修改的簡報寫入為 PPTX 檔案。
此 C++ 程式碼示範圖片框格式設定流程:
// 文件目錄的路徑。
const String outPath = u"../out/AddRelativeScaleHeightPictureFrame_out.pptx";
const String filePath = u"../templates/Tulips.jpg";
// 載入所需的簡報
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// 取得第一張投影片
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// 載入將加入簡報影像集合的圖像
// 取得圖片
auto image = Images::FromFile(filePath);
// 將影像加入簡報的影像集合
SharedPtr<IPPImage> imgx = pres->get_Images()->AddImage(image);
// 將圖片框加入投影片
SharedPtr<IPictureFrame> pf = slide->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, 50, 50, 100, 100, imgx);
// 設定相對縮放的寬度與高度
pf->set_RelativeScaleHeight (0.8);
pf->set_RelativeScaleWidth(1.35);
// 將 PPTX 檔案寫入磁碟
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);
將影像作為連結加入
為了避免簡報檔案過大,您可以透過連結方式加入影像(或影片),而非將檔案直接嵌入簡報。此 C++ 程式碼示範如何將影像與影片加入佔位元:
auto presentation = System::MakeObject<Presentation>(u"input.pptx");
auto shapesToRemove = System::MakeObject<System::Collections::Generic::List<System::SharedPtr<IShape>>>();
auto shapes = presentation->get_Slides()->idx_get(0)->get_Shapes();
for (auto& autoShape : shapes)
{
if (autoShape->get_Placeholder() == nullptr)
continue;
switch (autoShape->get_Placeholder()->get_Type())
{
case Aspose::Slides::PlaceholderType::Picture:
{
auto pictureFrame = shapes->AddPictureFrame(Aspose::Slides::ShapeType::Rectangle, autoShape->get_X(), autoShape->get_Y(), autoShape->get_Width(), autoShape->get_Height(), nullptr);
pictureFrame->get_PictureFormat()->get_Picture()->set_LinkPathLong(u"https://upload.wikimedia.org/wikipedia/commons/3/3a/I.M_at_Old_School_Public_Broadcasting_in_October_2016_02.jpg");
shapesToRemove->Add(autoShape);
break;
}
case Aspose::Slides::PlaceholderType::Media:
{
auto videoFrame = shapes->AddVideoFrame(autoShape->get_X(), autoShape->get_Y(), autoShape->get_Width(), autoShape->get_Height(), u"");
videoFrame->get_PictureFormat()->get_Picture()->set_LinkPathLong(u"https://upload.wikimedia.org/wikipedia/commons/3/3a/I.M_at_Old_School_Public_Broadcasting_in_October_2016_02.jpg");
videoFrame->set_LinkPathLong(u"https://youtu.be/t_1LYZ102RA");
shapesToRemove->Add(autoShape);
break;
}
}
}
for (auto& shape : shapesToRemove)
{
shapes->Remove(shape);
}
presentation->Save(u"output.pptx", Aspose::Slides::Export::SaveFormat::Pptx);
裁剪影像
此 C++ 程式碼示範如何在投影片上裁剪既有影像:
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = System::MakeObject<Presentation>();
// 建立新的影像物件
auto newImage = presentation->get_Images()->AddImage(Images::FromFile(imagePath));
// 在投影片上新增圖片框
auto picFrame = presentation->get_Slides()->idx_get(0)->get_Shapes()->AddPictureFrame(Aspose::Slides::ShapeType::Rectangle, 100.0f, 100.0f, 420.0f, 250.0f, newImage);
// 裁剪影像(百分比值)
picFrame->get_PictureFormat()->set_CropLeft(23.6f);
picFrame->get_PictureFormat()->set_CropRight(21.5f);
picFrame->get_PictureFormat()->set_CropTop(3.0f);
picFrame->get_PictureFormat()->set_CropBottom(31.0f);
// 儲存結果
presentation->Save(outPptxFile, Aspose::Slides::Export::SaveFormat::Pptx);
刪除圖片框的裁剪區域
若您想刪除框中影像的裁剪區域,可使用 IPictureFillFormat::DeletePictureCroppedAreas() 方法。若不需裁剪,該方法會返回原始影像。
此 C++ 程式碼示範此操作:
System::SharedPtr<Presentation> presentation = System::MakeObject<Presentation>(u"PictureFrameCrop.pptx");
System::SharedPtr<ISlide> slide = presentation->get_Slide(0);
// 從第一張投影片取得 PictureFrame
System::SharedPtr<IPictureFrame> picFrame = System::AsCast<IPictureFrame>(slide->get_Shape(0));
// 刪除 PictureFrame 影像的裁剪區域並返回裁剪後的影像
System::SharedPtr<IPPImage> croppedImage = picFrame->get_PictureFormat()->DeletePictureCroppedAreas();
// 儲存結果
presentation->Save(u"PictureFrameDeleteCroppedAreas.pptx", SaveFormat::Pptx);
NOTE
IPictureFillFormat::DeletePictureCroppedAreas() 方法會將裁剪後的影像加入簡報影像集合。若該影像僅在已處理的 PictureFrame 中使用,此設定可減少簡報大小;否則,最終簡報中的影像數量會增加。
此方法在裁剪過程中會將 WMF/EMF 中繪圖檔轉換為點陣 PNG 影像。
壓縮影像
您可以使用 IPictureFillFormat::CompressImage() 方法壓縮簡報中的圖片。此方法會根據形狀大小與指定的解析度減少影像大小,並可選擇刪除裁剪區域。
它的作用類似於 PowerPoint 中 圖片格式 → 壓縮圖片 → 解析度 的功能。
以下 C++ 範例示範如何以目標解析度壓縮簡報中的影像,並可選擇移除裁剪區域:
auto presentation = System::MakeObject<Presentation>(u"demo.pptx");
auto slide = presentation->get_Slide(0);
auto pictureFrame = System::AsCast<IPictureFrame>(slide->get_Shape(0));
// 以 150 DPI(網頁解析度)為目標解析度壓縮影像,並移除裁剪區域。
bool result = pictureFrame->get_PictureFormat()->CompressImage(true, PicturesCompression::Dpi150);
// 檢查壓縮結果。
if (result)
{
System::Console::WriteLine(u"Image successfully compressed.");
}
else
{
System::Console::WriteLine(u"Image compression failed or no changes were necessary.");
}
presentation->Save(u"CompressedImage.pptx", SaveFormat::Pptx);
presentation->Dispose();
或直接使用自訂 DPI 值:
auto presentation = System::MakeObject<Presentation>(u"demo.pptx");
auto slide = presentation->get_Slide(0);
auto pictureFrame = System::AsCast<IPictureFrame>(slide->get_Shape(0));
// 壓縮影像至 150 DPI(網頁解析度),並移除裁剪區域。
pictureFrame->get_PictureFormat()->CompressImage(true, 150.0f);
presentation->Save(u"CompressedImage.pptx", SaveFormat::Pptx);
presentation->Dispose();
NOTE
此方法會根據形狀大小與提供的 DPI 將影像轉換為較低解析度。亦可刪除裁剪區域以最佳化檔案大小。若影像為 WMF/EMF 中繪圖檔或 SVG,則不會執行壓縮。JPEG 的品質會依解析度略有下降,行為與 PowerPoint 處理高解析度 JPEG 相同。鎖定長寬比例
若您希望包含影像的形狀在變更影像尺寸後仍保留長寬比例,可使用 set_AspectRatioLocked() 方法設定 Lock Aspect Ratio。
此 C++ 程式碼示範如何鎖定形狀的長寬比例:
System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>(u"pres.pptx");
System::SharedPtr<ILayoutSlide> layout = pres->get_LayoutSlides()->GetByType(SlideLayoutType::Custom);
System::SharedPtr<ISlide> emptySlide = pres->get_Slides()->AddEmptySlide(layout);
System::SharedPtr<IImage> image = Images::FromFile(u"image.png");
System::SharedPtr<IPPImage> presImage = pres->get_Images()->AddImage(image);
System::SharedPtr<IPictureFrame> pictureFrame = emptySlide->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, 50.0f, 150.0f, static_cast<float>(presImage->get_Width()), static_cast<float>(presImage->get_Height()), presImage);
// 設定形狀在調整大小時保持長寬比例
pictureFrame->get_PictureFrameLock()->set_AspectRatioLocked(true);
NOTE
此 Lock Aspect Ratio 設定僅保留形狀的長寬比例,並不會影響其中的影像。使用 StretchOff 屬性
使用 IPictureFillFormat 介面與 PictureFillFormat 類別中的 StretchOffsetLeft、StretchOffsetTop、StretchOffsetRight 以及 StretchOffsetBottom 屬性,您可以指定填充矩形。
當指定影像伸展時,來源矩形會縮放以適應指定的填充矩形。填充矩形的每一邊皆以相對於形狀邊界框相應邊的百分比偏移定義。正百分比表示內縮,負百分比表示外伸。
- 建立 Presentation 類別的實例。
- 透過索引取得投影片的參考。
- 新增矩形
AutoShape。 - 建立影像。
- 設定形狀的填充類型。
- 設定形狀的圖片填充模式。
- 新增設定好的影像以填充形狀。
- 指定影像相對於形狀邊界框相應邊的偏移。
- 將已修改的簡報寫入為 PPTX 檔案。
此 C++ 程式碼示範使用 StretchOff 屬性的流程:
auto pres = System::MakeObject<Presentation>();
auto ppImage = pres->get_Images()->AddImage(Images::FromFile(u"image.png"));
auto slide = pres->get_Slide(0);
auto pictureFrame = slide->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, 10.0f, 10.0f, 400.0f, 400.0f, ppImage);
// 設定影像在形狀內部從每一側伸展
auto pictureFormat = pictureFrame->get_PictureFormat();
pictureFormat->set_PictureFillMode(PictureFillMode::Stretch);
pictureFormat->set_StretchOffsetLeft(24.0f);
pictureFormat->set_StretchOffsetRight(24.0f);
pictureFormat->set_StretchOffsetTop(24.0f);
pictureFormat->set_StretchOffsetBottom(24.0f);
pres->Save(u"imageStretch.pptx", SaveFormat::Pptx);
常見問題
如何得知 PictureFrame 支援哪些影像格式?
Aspose.Slides 支援點陣圖(PNG、JPEG、BMP、GIF 等)與向量圖(例如 SVG),透過指派給 PictureFrame 的影像物件實現。支援的格式列表通常與投影片與影像轉換引擎的能力重疊。
大量加入大尺寸影像會如何影響 PPTX 大小與效能?
嵌入大尺寸影像會增加檔案大小與記憶體使用量;以連結方式加入影像可減少簡報檔案大小,但需確保外部檔案仍可存取。Aspose.Slides 提供以連結方式加入影像的功能,以降低檔案大小。
如何防止影像物件被意外移動或調整大小?
對 PictureFrame 使用 shape locks(例如停用移動或調整大小)。相關鎖定機制在另一篇 保護文章 中說明,支援各種形狀類型,包括 PictureFrame。
匯出簡報為 PDF/影像時,SVG 向量保真度是否得以保留?
Aspose.Slides 允許從 PictureFrame 提取原始 SVG 向量。若 匯出為 PDF 或 點陣格式,結果可能會依匯出設定被點陣化;但提取行為證實原始 SVG 仍以向量形式存儲。