如何在 PowerPoint 中管理圖片
多媒體元素是 PowerPoint 簡報不可或缺的組成部分。 圖片尤其能提供視覺脈絡,並強化每張投影片所呈現的資訊。有效的圖片管理(無論是插入新視覺素材、更新現有圖片,或是清理過時的圖形)對於維持Professional且可擴展的簡報至關重要。
本指南將示範如何使用 IronPPT 透過程式化方式處理圖片。
如何在 PowerPoint 中管理圖片
- 下載用於 PowerPoint 自動化的 C# 函式庫
- 載入或建立 PowerPoint 簡報
- 使用
Image類別處理影像物件 - 執行操作:插入、修改、替換或移除圖片
- 儲存並匯出為 PPTX
新增圖片
若要使用 IronPPT 在 PowerPoint 文件中插入圖片,請建立新的文件物件(或從現有檔案載入)。 接著,從 Image 類別建立一個引用檔案的影像物件。影像載入後,將其加入文件並指定應顯示的投影片編號。 在此基礎上,可透過 Width 及 Angle 等屬性來調整圖片屬性。 最後,請將文件連同新加入的圖片一併匯出。
: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");
Imports IronPPT
Imports IronPPT.Models
' Create a new presentation document
Dim document As New PresentationDocument()
' Create and load an image from file
Dim image As New Image()
image.LoadFromFile("image.jpg")
' Add image to the first slide (index 0)
Dim 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")
圖片屬性
請參閱下表中的圖片屬性選項。
| 屬性 | 描述 | 範例 |
|---|---|---|
高度 |
設定圖片的高度(單位為點)。 | image.Height = 300; |
寬度 |
設定圖片的寬度(單位為點)。 | image.Width = 400; |
Angle |
將圖片旋轉指定角度(以度為單位)。 | image.Angle = 45; |
職位 |
使用 x 和 y 座標設定圖片在投影片上的位置。 | image.Position = (200, 200); |
FrameShape |
使用 ShapeType 枚舉值設定影像的框架形狀。 | image.FrameShape = IronPPT.Enums.ShapeType.RoundRectangle; |
修改新增圖片的屬性
將圖片新增至投影片後,您可以修改其屬性以調整外觀與位置。 例如,使用 Width 和 Angle 等屬性來自訂圖片尺寸與旋轉角度。 調整這些設定可讓您微調圖片在簡報中的顯示效果。
: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");
Imports IronPPT
Imports IronPPT.Models
Imports IronPPT.Enums
' Load an existing presentation document
Dim document As New PresentationDocument("existing-presentation.pptx")
' Create and load an image from file
Dim image As New Image()
image.LoadFromFile("image.jpg")
' Add image to the second slide (index 1)
Dim 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")
替換圖片
使用 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");
Imports IronPPT
Imports IronPPT.Models
' Load an existing presentation
Dim document As New PresentationDocument("sample.pptx")
' Load the replacement image
Dim replaceImage As 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")
原文
結果
移除圖片(按索引排序)
移除圖片最簡單的方法是透過其索引位置。 請存取該投影片的圖片集合,並使用 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");
Imports IronPPT
' Create a new presentation
Dim document As 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")
移除前圖片
移除圖片後
移除所有圖片
若需批量刪除文件中所有 Image 檔案,可使用兩個 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");
Imports IronPPT
Imports IronPPT.Models
' Load an existing presentation
Dim document As New PresentationDocument("real_sample.pptx")
' Remove all images from every slide
For s As Integer = 0 To document.Slides.Count - 1 ' Loop through all slides
Dim slide = document.Slides(s) ' Get the current slide
For i As Integer = slide.Images.Count - 1 To 0 Step -1 ' Loop backward through images on this slide
slide.Images(i).Remove() ' Remove each image
Next
Next
' Save the updated presentation
document.Save("removed-images.pptx")
批量刪除前
批量刪除後
如您所見,第 2 頁和第 4 頁的圖片均已移除。
常見問題
在 PowerPoint 簡報中插入圖片時,可以使用哪些圖片檔案格式?
IronPPT 支援常見的圖像格式,包括 JPEG、PNG、BMP、GIF 和 TIFF。當將圖像加入簡報時,此程式庫會自動處理格式轉換與優化,確保與大多數圖像來源的相容性。
如何在簡報的特定投影片中加入圖片?
若要使用 IronPPT 插入圖片,請先透過 Image.Create() 並傳入圖片檔案路徑來建立圖片物件,接著使用 slide.Images.Add() 將其加入特定投影片。您可以透過索引存取投影片,例如:ppt.Slides[0].Images.Add(image) 會將圖片加入第一張投影片。
我可以透過程式設計來控制圖片的大小和尺寸嗎?
是的,IronPPT 允許您透過 Image 物件的 Width 和 Height 屬性來設定圖片尺寸。只需在將圖片新增至投影片之前或之後,將這些屬性設定為點數值即可,例如 image.Width = 400 和 image.Height = 300。
如何將圖片定位在投影片的特定位置?
IronPPT 透過 Position 屬性搭配 x、y 座標,實現精確的圖片定位。座標系統以左上角 (0,0) 為起點,單位為點 (point),讓您能將圖片放置在投影片畫面的任何位置。
是否可以替換 PowerPoint 簡報中的現有圖片?
是的,IronPPT 支援替換簡報中的現有圖片。您可以標記要替換的圖片,並將其替換為新的圖片物件,同時維持相同的位置和屬性,確保視覺內容的無縫更新。
我可以透過程式碼從投影片中移除圖片嗎?
IronPPT 提供從簡報中個別或批量移除圖片的功能。您可以透過存取投影片的 Images 集合,並使用適當的移除方法來移除特定圖片。

