如何在 PowerPoint 中管理圖像

This article was translated from English: Does it need improvement?
Translated
View the article in English

多媒體元素構成了 PowerPoint 簡報不可或缺的結構。 圖片尤其能夠提供視覺背景,並強化每張投影片上呈現的訊息。有效的圖片管理(無論是插入新圖片、更新現有圖片還是清理過時的圖片)對於保持簡報的專業性和可擴展性至關重要。

本指南示範如何使用 IronPPT 以程式處理影像。

開始使用 IronPPT

立即開始在您的項目中使用 IronPPT 並免費試用。

第一步:
green arrow pointer


新增圖片

若要使用 IronPPT 在 PowerPoint 文件中新增圖像,請建立新的文件物件(或從現有文件載入)。 然後,使用Image類別建立一個引用檔案的圖像物件。影像載入完成後,將其新增至文件中,並指定其顯示的幻燈片編號。 從那裡,可以使用HeightWidthAngle等屬性來更改圖像屬性。 最後,匯出包含新新增影像的文件。

:path=/static-assets/ppt/content-code-examples/how-to/manage-image-add-image-add-image.cs
using IronPPT;
using IronPPT.Models;

// Create a new presentation document
var document = new PresentationDocument();

// Create and load an image from file
Image image = new Image();
image.LoadFromFile("image.jpg");

// Add image to the first slide (index 0)
var newImage = document.AddImage(image, 0);

// Rotate the image 180 degrees
newImage.Angle = 180;

// Save the presentation as a .pptx file
document.Save("adding-image.pptx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel
在 PowerPoint 投影片中新增圖像

圖像屬性

請查看下表中的圖像屬性選項。

屬性 描述 例子
Height 設定圖像高度(以磅為單位)。 image.Height = 300;
Width 設定影像寬度(以磅為單位)。 image.Width = 400;
Angle 將影像旋轉指定的角度(以度為單位)。 image.Angle = 45;
Position 使用 x 和 y 座標設定影像在投影片上的位置。 image.Position = (200, 200);
FrameShape 使用 ShapeType 枚舉值設定影像的框架形狀。 image.FrameShape = IronPPT.Enums.ShapeType.RoundRectangle;

修改已新增的圖像屬性

將圖片新增至投影片後,您可以修改其屬性以調整外觀和位置。 例如,使用HeightWidthAngle等屬性來自訂圖像尺寸和旋轉。 調整這些設定可以讓你微調影像在簡報中的顯示方式。

:path=/static-assets/ppt/content-code-examples/how-to/manage-image-add-image-modify-properties.cs
using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;

// Load an existing presentation document
var document = new PresentationDocument("existing-presentation.pptx");

// Create and load an image from file
Image image = new Image();
image.LoadFromFile("image.jpg");

// Add image to the second slide (index 1)
var newImage = document.AddImage(image, 1);

// Modify image properties
newImage.Angle = 45; // Rotate the image 45 degrees
newImage.FrameShape = ShapeType.RoundRectangle; // Set the frame shape to Rounded Rectangle
newImage.Position = (180, 180); // Set the position to coordinates (180, 180)
newImage.Width = 300; // Set the width to 300 points
newImage.Height = 300; // Set the height to 300 points

// Save the modified presentation as a new .pptx file
document.Save("modifying-image-properties.pptx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel
在 PowerPoint 中修改圖片屬性

替換影像

使用 IronPPT 取代圖片是一項直覺的操作。 首先,將簡報文件和新圖像載入到一個新的Image物件中。 然後,透過選擇要更新的圖像的幻燈片和索引來定位該圖像,例如Slides[0].Images[0] (對於第一張幻燈片上的第一張圖像)。 完成後,使用新的圖像物件呼叫Replace方法並匯出檔案。

:path=/static-assets/ppt/content-code-examples/how-to/manage-image-replace-image-replace-image.cs
using IronPPT;
using IronPPT.Models;

// Load an existing presentation
var document = new PresentationDocument("sample.pptx");

// Load the replacement image
Image replaceImage = new Image();
replaceImage.LoadFromFile("sample.png");

// Replace the first image found in the first slide
document.Slides[0].Images[0].Replace(replaceImage);

// Save changes (overwriting the original file)
document.Save("sample.pptx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

原來的

替換 PowerPoint 投影片中的影像(替換前)

結果

替換 PowerPoint 投影片中的影像(替換後)

按索引刪除影像

刪除圖像最簡單的方法是按其索引位置刪除。 存取幻燈片的圖像集合,並使用Remove方法和要刪除的圖像的從零開始的索引。 當您知道影像在影像集中的確切位置時,這種方法非常有效。

:path=/static-assets/ppt/content-code-examples/how-to/manage-image-remove-image-remove-by-index.cs
using IronPPT;

// Create a new presentation
var document = new PresentationDocument("real_sample.pptx");

// Remove the first image found in the first slide
document.Slides[1].Images[0].Remove();

// Save the updated presentation
document.Save("removed-image.pptx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

移除影像前

從 PowerPoint 投影片中按索引刪除影像(檢視之前)

移除影像後

從 PowerPoint 幻燈片中按索引刪除圖像(查看後)

刪除所有圖片

對於需要批量刪除文件中所有圖像文件的情況,我們可以使用兩個for循環:一個循環遍歷所有文檔頁面,兩個循環再次遍歷並刪除每個頁面上所有已識別的圖像。 下面給出一個例子。

:path=/static-assets/ppt/content-code-examples/how-to/manage-image-remove-all-images.cs
using IronPPT;
using IronPPT.Models;

// Load an existing presentation
var document = new PresentationDocument("real_sample.pptx");

// Remove all images from every slide
for (int s = 0; s < document.Slides.Count; s++)       // Loop through all slides
{
    var slide = document.Slides[s];                   // Get the current slide

    for (int i = slide.Images.Count - 1; i >= 0; i--) // Loop backward through images on this slide
    {
        slide.Images[i].Remove();                     // Remove each image
    }
}

// Save the updated presentation
document.Save("removed-images.pptx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

批量刪除之前

批次從 PowerPoint 投影片中按索引刪除影像(檢視之前)

批量刪除後

如您所見,第 2 張和第 4 張投影片中的所有影像都已刪除。

批次從 PowerPoint 幻燈片中刪除按索引排列的圖像(檢視後)

常見問題解答

如何使用 IronPPT 在 PowerPoint 投影片中加入圖片?

使用 IronPPT,您可以利用其直觀的 API 在任何幻燈片的特定位置插入圖片,將圖片添加到 PowerPoint 幻燈片中。

使用 IronPPT 在 PowerPoint 中移除圖片的流程為何?

IronPPT 可讓您從 PowerPoint 投影片中移除圖片,方法是識別圖片物件,並使用適當的 API 呼叫將其從簡報中刪除。

我可以使用 IronPPT 取代 PowerPoint 簡報中的現有圖片嗎?

是的,IronPPT 提供更換現有圖片的功能,移除舊圖片並在其位置上新增新圖片,確保您的簡報內容保持最新。

IronPPT 支援在單一幻燈片上管理多張圖片嗎?

IronPPT 支援在單一幻燈片上管理多張圖片,讓您可以根據需要新增、移除或替換圖片,以客製化您的簡報。

IronPPT 支援哪些 PowerPoint 投影片的影像格式?

IronPPT 支援多種圖像格式,包括 JPEG、PNG 和 BMP,讓您可以在 PowerPoint 簡報中使用多種圖像類型。

是否可以使用 IronPPT 在 PowerPoint 中調整圖片大小?

是的,IronPPT 提供調整圖片大小的功能,讓您可以調整圖片尺寸,使其完全符合您的幻燈片版面。

IronPPT 如何處理 PowerPoint 投影片中的影像透明度?

IronPPT 支援圖片透明度,讓您可以將透明背景的圖片無縫納入 PowerPoint 簡報中。

IronPPT 可以用來在 PowerPoint 簡報的批次處理中管理影像嗎?

IronPPT 具備批次處理的能力,可讓您有效率地管理數張幻燈片上的多張影像。

艾哈邁德·索海爾
全栈开发者

Ahmad 是一位全端開發人員,精通 C#、Python 和 Web 技術。他對建立可擴展的軟體解決方案有著濃厚的興趣,並樂於探索如何在實際應用中實現設計與功能的完美結合。

在加入 Iron Software 團隊之前,Ahmad 曾從事自動化專案和 API 整合工作,專注於提高效能和開發者體驗。

在空閒時間,他喜歡嘗試 UI/UX 設計理念,為開源工具做出貢獻,偶爾還會涉足技術寫作和文件編寫,使複雜的主題更容易理解。

準備好開始了嗎?
Nuget 下載 3,325 | Version: 2025.11 剛發表