如何在 PowerPoint 中管理圖像
多媒體元素構成了 PowerPoint 簡報不可或缺的結構。 圖片尤其能夠提供視覺背景,並強化每張投影片上呈現的訊息。有效的圖片管理(無論是插入新圖片、更新現有圖片還是清理過時的圖片)對於保持簡報的專業性和可擴展性至關重要。
本指南示範如何使用 IronPPT 以程式處理影像。
開始使用 IronPPT
!{--01001100010010010100001001010010010000010101001001011001010111110101001101010100010001010101010 10100010111110101010001010010010010010100000101001100010111110100001001001100010011111010000100100110001001111010101
如何在 PowerPoint 中管理圖像
- 下載用於 PowerPoint 自動化的 C# 庫
- 載入或建立 PowerPoint 簡報
- 使用
Image類別處理影像對象 - 執行操作:插入、修改、取代或刪除影像
- 儲存並匯出為 PPTX 格式
新增圖片
若要使用 IronPPT 在 PowerPoint 文件中新增圖像,請建立新的文件物件(或從現有文件載入)。 然後,使用Image類別建立一個引用檔案的圖像物件。影像載入完成後,將其新增至文件中,並指定其顯示的幻燈片編號。 從那裡,可以使用Height 、 Width和Angle等屬性來更改圖像屬性。 最後,匯出包含新新增影像的文件。
:path=/static-assets/ppt/content-code-examples/how-to/manage-image-add-image-add-image.csusing 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
圖像屬性
請查看下表中的圖像屬性選項。
| 屬性 | 描述 | 例子 |
|---|---|---|
高度 | 設定圖像高度(以磅為單位)。 | image.Height=300;。 |
寬度 | 設定影像寬度(以磅為單位)。 | image.Width=400;。 |
Angle | 將影像旋轉指定的角度(以度為單位)。 | image.Angle=45;。 |
職位 | 使用 x 和 y 座標設定影像在投影片上的位置。 | image.Position = (200, 200); |
框架形狀 | 使用 ShapeType 枚舉值設定影像的框架形狀。 | image.FrameShape=IronPPT.Enums.ShapeType.RoundRectangle;。 |
修改已新增的圖像屬性
將圖片新增至投影片後,您可以修改其屬性以調整外觀和位置。 例如,使用Height 、 Width和Angle等屬性來自訂圖像尺寸和旋轉。 調整這些設定可以讓你微調影像在簡報中的顯示方式。
:path=/static-assets/ppt/content-code-examples/how-to/manage-image-add-image-modify-properties.csusing 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
替換影像
使用 IronPPT 取代圖片是一項直覺的操作。 首先,將簡報文件和新圖像載入到一個新的Image物件中。 然後,透過選擇要更新的圖像的幻燈片和索引來定位該圖像,例如Slides[0].Images[0] (對於第一張幻燈片上的第一張圖像)。 完成後,使用新的圖像物件呼叫Replace方法並匯出檔案。
:path=/static-assets/ppt/content-code-examples/how-to/manage-image-replace-image-replace-image.csusing 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原來的

結果

按索引刪除影像
刪除圖像最簡單的方法是按其索引位置刪除。 存取幻燈片的圖像集合,並使用Remove方法和要刪除的圖像的從零開始的索引。 當您知道影像在影像集中的確切位置時,這種方法非常有效。
:path=/static-assets/ppt/content-code-examples/how-to/manage-image-remove-image-remove-by-index.csusing 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移除影像前

移除影像後

刪除所有圖片
對於需要大量刪除文件中所有 圖片 檔案的情況,我們可以使用兩個 for 環路:一次遍歷所有文件頁面,兩次重申並刪除每頁中所有已識別的圖片。 下面給出一個例子。
:path=/static-assets/ppt/content-code-examples/how-to/manage-image-remove-all-images.csusing 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批量刪除之前

批量刪除後
如您所見,第 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 可使用具有 x,y 座標的 Position 屬性來進行精確的影像定位。坐標系統從左上角 (0,0) 開始,數值以點為單位,讓您可以將影像放置在幻燈片表面的任何位置。
是否可以取代 PowerPoint 簡報中的現有圖片?
是的,IronPPT 支援取代簡報中的現有圖片。您可以識別要取代的圖像,並以新的圖像物件取代,同時維持相同的位置和屬性,確保視覺內容的無縫更新。
我可以用程式移除幻燈片上的圖片嗎?
IronPPT 提供從簡報中個別或大量移除圖片的功能。您可以透過存取幻燈片的圖片集合,並使用適當的移除方法來移除特定的圖片。







