如何在 PowerPoint 中管理圖像
多媒體元素構成了 PowerPoint 簡報不可或缺的結構。 圖片尤其能夠提供視覺背景,並強化每張投影片上呈現的訊息。有效的圖片管理(無論是插入新圖片、更新現有圖片還是清理過時的圖片)對於保持簡報的專業性和可擴展性至關重要。
本指南示範如何使用 IronPPT 以程式處理影像。
如何在 PowerPoint 中管理圖像
- 下載用於 PowerPoint 自動化的 C# 庫
- 載入或建立 PowerPoint 簡報
- 使用
Image類別處理影像對象 - 執行操作:插入、修改、取代或刪除影像
- 儲存並匯出為 PPTX 格式
新增圖片
若要使用 IronPPT 在 PowerPoint 文件中新增圖像,請建立新的文件物件(或從現有文件載入)。 然後,從參考檔案的Image類別創建一個圖像物件。圖像載入後,將其添加到文件中並指定應該出現的幻燈片編號。 從那裡,您可以使用例如Height、Width及Angle等屬性來改變圖片屬性。 最後,匯出包含新新增影像的文件。
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")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")
圖像屬性
請查看下表中的圖像屬性選項。
| 屬性 | 描述 | 例子 |
|---|---|---|
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;。 |
修改已新增的圖像屬性
將圖片新增至投影片後,您可以修改其屬性以調整外觀和位置。 例如,使用Height、Width及Angle等屬性來自訂圖片的尺寸和旋轉。 調整這些設定可以讓您微調圖像在簡報中的顯示方式。
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");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")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方法並匯出文件。
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");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")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方法。 當您知道影像在影像集中的確切位置時,這種方法非常有效。
using IronPPT;
// Load an existing presentation
var document = new PresentationDocument("real_sample.pptx");
// Remove the first image found on the second slide
document.Slides[1].Images[0].Remove();
// Save the updated presentation
document.Save("removed-image.pptx");using IronPPT;
// Load an existing presentation
var document = new PresentationDocument("real_sample.pptx");
// Remove the first image found on the second slide
document.Slides[1].Images[0].Remove();
// Save the updated presentation
document.Save("removed-image.pptx");Imports IronPPT
' Load an existing presentation
Dim document As New PresentationDocument("real_sample.pptx")
' Remove the first image found on the second slide
document.Slides(1).Images(0).Remove()
' Save the updated presentation
document.Save("removed-image.pptx")Imports IronPPT
' Load an existing presentation
Dim document As New PresentationDocument("real_sample.pptx")
' Remove the first image found on the second slide
document.Slides(1).Images(0).Remove()
' Save the updated presentation
document.Save("removed-image.pptx")移除影像前

移除影像後

刪除所有圖片
對於需要批量刪除文件中所有Image檔案的情境,我們可以使用兩個for迴圈:一次用來遍歷所有文件頁面,第二次用來重迭並移除每頁所識別的圖片。 下面給出一個例子。
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");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")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 張投影片中的所有影像都已刪除。

常見問題
如何使用IronPPT將圖片新增到PowerPoint投影片中?
要使用IronPPT添加圖片,首先建立或載入PowerPoint文件。使用Image類建立圖片物件,將您想要的圖片檔案載入其中,然後使用文件的AddImage方法將此圖片添加到幻燈片上。在將文件儲存為PPTX檔案前,您可以調整Height、Width和Angle等屬性。
使用IronPPT可以在PowerPoint上對圖片執行哪些操作?
IronPPT允許的操作包括插入新圖片、修改現有圖片屬性、更換圖片以及從PowerPoint簡報中移除圖片。
加入圖片到PowerPoint投影片後,能否修改圖片屬性?
是的,一旦使用IronPPT將圖片添加到PowerPoint幻燈片上,您可以修改其屬性,例如Height、Width、Angle、Position和FrameShape,以調整其外觀和在幻燈片上的位置。
如何使用IronPPT替換PowerPoint簡報中的圖片?
要使用IronPPT在PowerPoint演示文稿中替換圖片,載入演示文稿文件和新圖片,透過幻燈片和索引存取目標圖片,然後使用Replace方法替換為新圖片。記得儲存您的更改。
如何通過索引使用IronPPT移除圖片?
要在IronPPT中按索引移除圖片,存取幻燈片的圖片集合,使用Remove方法和圖片的零基索引,然後儲存更新後的演示文稿。
如何使用IronPPT從PowerPoint簡報中移除所有圖片?
使用IronPPT從PowerPoint簡報中移除所有圖片涉及逐一遍歷每張投影片,並使用反向迴圈通過索引移除每張圖片。
為什麼應該以程式化方式管理PowerPoint簡報中的圖片?
使用IronPPT程式化管理圖片可以提高效率,確保定位和尺寸的精準性,並允許動態更新,這對於專業和可擴展的簡報至關重要。
在IronPPT中,`Image`類用於什麼?
在IronPPT中,Image類用於建立可以從檔案載入且在添加到PowerPoint幻燈片之前可設定不同屬性加以操作的圖片物件。
是否可以使用IronPPT匯出新的圖片的修改後的PowerPoint?
可以,當您在PowerPoint中做了圖片的修改或新增後,您可以使用IronPPT將簡報匯出為PPTX文件,保持您的所有調整。
使用IronPPT可以調整哪些常見的圖片屬性?
常見的圖片屬性包括Height、Width、Angle、Position和FrameShape,這允許對圖片外觀和在幻燈片上的放置進行全面的自訂。
Ahmad 是一位全端開發人員,精通 C#、Python 和 Web 技術。他對建立可擴展的軟體解決方案有著濃厚的興趣,並樂於探索如何在實際應用中實現設計與功能的完美結合。