IRONSOFTWAREHOME

如何在C#中將圖像新增到DOCX

Ahmad Sohail
Ahmad Sohail
Updated: 2026年5月9日

IronWord提供ImageContent類別,用於將圖像(.jpg、.png、.bmp、.tiff、.gif)插入到DOCX文件中,並可以自定義屬性如寬度、高度和文字環繞。 使用IronWord將圖像新增到Word文件中,以進行文件自動化和報告生成。

快速入門:在C#中將圖像新增到DOCX
  1. 通過NuGet程式庫管理員安裝IronWord
  2. 建立一個新的WordDocument實例
  3. 使用ImageContent類載入圖像
  4. 使用AddImage()將圖像新增到文件中
  5. 將文件保存為DOCX
  1. 1Install IronWord with NuGet Package Manager

    PM > Install-Package IronWord

  2. 2複製並運行這段程式碼片段。

    using IronWord;
    using IronWord.Models;
    
    // Create new document
    WordDocument doc = new WordDocument();
    
    // Add image
    ImageContent image = new ImageContent("photo.jpg");
    doc.AddImage(image);
    
    // Save document
    doc.SaveAs("document-with-image.docx");
    C#
  3. 3部署以在您的實時環境中測試

    今天就開始在您的專案中使用IronWord,透過免費試用
    arrow pointer

試試IronWord


如何將圖像新增到DOCX中?

使用文件路徑引用圖像。 首先,使用文件路徑作為字串實例化ImageContent類。 在整個文件中使用image變數來修改屬性,如寬度和高度。 使用AddImage()函式將圖像新增到.docx文件中。 將文件在本地導出並保存。

下面的範例在沒有任何父節點的情況下向文件中新增圖像。 支持的文件格式包括.jpg、.png、.bmp、.tiff和.gif。 此靈活性允許您處理任何常見的圖像格式。

提示: 將圖像作為子元素插入到段落中以獲得更好的文件層次結構。 段落定義了文字環繞和其他文字格式屬性。 這種方法提供了對圖像位置的更多控制,並允許圖像隨著周圍文字流動。
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

// initializing docx file
WordDocument doc = new IronWord.WordDocument();

// instantiating image file
IronWord.Models.ImageContent image = new IronWord.Models.ImageContent("sample-image.jpg");

// modifying image properties
image.Width = 200;
image.Height = 200;

// AddImage function saving the image
doc.AddImage(image);

// Save and export the file
doc.SaveAs("inserted-image.docx");
Word文件顯示插入的夜間城市天際線圖像,Home標籤頁功能區顯示格式選項

支持哪些圖像格式?

支持的文件格式:.jpg、.png、.bmp、.tiff和.gif。 每種格式在插入時保持其質量。 JPEG最適合照片。PNG支持透明度,用於標誌和圖形。 BMP提供無壓縮的質量。 TIFF適合高質量的列印文件。 GIF允許簡單動畫(僅在靜態文件中顯示第一幀)。

圖像放在哪裡?

預設情況下,圖像新增到當前光標位置,沒有任何父節點。 為了精確定位,將圖像作為子元素插入到段落中。這樣可以更好地控制文字流動,並將圖像與您的文件結構整合在一起。

為什麼要使用ImageContent類?

ImageContent類以結構化方式管理圖像屬性。 在插入之前修改尺寸、位置和格式。 這種方法確保了文件生成過程的一致性,並將標準格式規則應用於整個應用程式。 該類封裝了所有與圖像相關的屬性,使程式碼更易於維護並減少格式錯誤。

如何通過流的方式新增圖像?

本地或靜態URL的圖像可以很容易地使用上述方法新增。 然而,應用程式通常會使用來自資料庫、網路服務或動態生成的內容圖像。 使用Stream方法新增需要身份驗證的安全API後的圖像。

下面的範例顯示HTTP客戶端發送授權標記以檢索經過身份驗證的圖像流。 流在導出之前直接整合到文件中。 此方法消除了暫時文件儲存,並提高了對敏感圖像資料的安全性。

using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

// initializing docx file
WordDocument doc = new IronWord.WordDocument();

using (HttpClient client = new HttpClient())
{
    // Add authentication headers
    client.DefaultRequestHeaders.Add("Authorization", "Bearer YOUR_API_KEY_HERE");
    client.DefaultRequestHeaders.Add("User-Agent", "MyApp/1.0");

    // Get image from authenticated endpoint
    Stream authenticatedStream = await client.GetStreamAsync("https://api.example.com/secure/image.png");
    doc.AddImage(authenticatedStream);
}

// Export docx
doc.SaveAs("added-image-via-http-stream.docx");

我什麼時候應該使用流方法?

當以下情況時使用Stream方法:

  • 圖像在需要身份驗證的安全API後
  • 從記憶體動態處理圖像
  • 處理儲存在資料庫中的二進制資料圖像

此方法在企業應用程式中效果很好,特別是圖像儲存在文件管理系統、雲儲存中,或由圖像處理服務生成。

流載入的好處是什麼?

流載入從經過身份驗證的端點整合圖像,而不需要保存暫存檔。 這提高了安全性和性能。 好處包括:

  • 減少磁碟I/O操作
  • 沒有敏感圖像的磁碟快取
  • 實時圖像處理工作流程
  • 更好的大圖像記憶體管理
  • 靈活的圖像來源選項

我如何修改圖像屬性?

IronWord提供全面的方法來自定義圖像屬性。 在將圖像新增到文件之前或之後調整這些屬性。

設定描述範例
Width圖像的水平方向像素尺寸image.Width = 500;
Height圖像的垂直方向像素尺寸image.Height = 300;
WrapText圖像的文字環繞行為image.WrapText = WrapText.Square;
DistanceFromLeft從左邊緣到的距離測量,以像素為單位image.DistanceFromLeft = 10;
DistanceFromRight從右邊緣到的距離測量,以像素為單位image.DistanceFromRight = 10;
DistanceFromTop從上邊緣到的距離測量,以像素為單位image.DistanceFromTop = 15;
DistanceFromBottom從下邊緣到的距離測量,以像素為單位image.DistanceFromBottom = 15;
Position空間放置資訊(X和Y座標)image.Position = new ElementPosition(50, 100);
ScaleX和Y軸比例調整因子image.Scale = new PointF(1.5f, 1.5f);
Translate重新定位的位移座標image.Translate = new PointF(20, 30);

如何自訂寬度和高度?

通過改變長寬比來實現自訂寬度和高度。 控制圖像在文件中的顯示方式,不論是維持比例還是適應特定佈局限制。

using IronWord;

// initializing docx file
WordDocument doc = new IronWord.WordDocument();

// instantiating image file
IronWord.Models.ImageContent image = new IronWord.Models.ImageContent("sample-image.tiff");

// modifying the aspect ratio by introducing custom width
image.Width = 800;
image.Height = 200;

// AddImage function saving the image
doc.AddImage(image);

// Save and export the file
doc.SaveAs("custom-size-image.docx");
Word文件顯示筆記本電腦圖片,顯示標記的圖像文件F疊加文字,展示圖像屬性顯示

長寬比會如何?

自訂的寬度和高度值將覆蓋原始的長寬比。 拉伸或壓縮圖像以適應如頁眉、側邊欄或固定大小的容器等佈局需求。 極端的變形可能會顯得不專業。 要在調整大小時保持長寬比,根據目標尺寸計算比例尺寸。

我應該先設置哪些屬性?

按以下順序設置屬性:

  1. 尺寸(寬度/高度)- 圖像佈局的基礎
  2. 定位(DistanceFrom屬性)- 控制距離和邊距
  3. 高級屬性(縮放/平移)- 微調

這種方法確保每個屬性邏輯地基於先前的屬性構建。 某些屬性會互相作用 - 文字環繞會影響距離屬性的效果。

常見問題

如何在C#中將圖像新增到DOCX文件?

使用IronWord,您可以通過為您的圖像文件路徑建立一個ImageContent實例,然後使用AddImage()方法將圖像新增到DOCX文件中。IronWord支持常用格式如JPG、PNG、BMP、TIFF和GIF,使得程式方式插入圖像到Word文件中變得容易。

在將圖像新增到Word文件時支持哪些圖像格式?

IronWord支持所有主要圖像格式,包括.jpg、.png、.bmp、.tiff和.gif文件。每種格式在插入時保持其質量 - JPEG用於照片,PNG用於透明圖形,BMP用於無壓縮質量,TIFF用於高質量印刷文件,GIF用於簡單的動畫(僅顯示第一幀)。

我可以控制DOCX文件中圖像的大小和位置嗎?

是的,IronWord的ImageContent類讓您可以自定義圖像屬性,例如寬度、高度和文字環繞。您可以在插入前修改尺寸和位置,讓您對Word文件中圖像的顯示有完全控制。

如何在段落中插入圖像以改善文件結構?

IronWord允許您將圖像作為段落的子元素插入,提供更好的文件層次和對文字環繞的控制。這種方式讓圖像與周圍文字的流動結合,比沒有父節點新增圖像提供更精確的定位選項。

什麼是最快的程式方式將圖像新增到Word文件的方法?

使用IronWord,最快的方法是建立一個WordDocument實例,使用新的ImageContent('photo.jpg')載入您的圖像,調用doc.AddImage(image),並用doc.SaveAs('document-with-image.docx')保存。這個簡單的四步驟過程處理了整個圖像插入工作流程。

How do I place images at specific positions within a DOCX file using IronWord?

Images in IronWord are initially placed at the current cursor position. For precise positioning, insert images as child elements within paragraphs to manage text flow and layout.

Can I modify text wrapping settings for images in IronWord?

Yes, IronWord allows you to set text wrapping for images using the `TextWrapBehavior` property. This controls how text flows around the inserted images within the DOCX file.

Why is it beneficial to use the `ImageContent` class in IronWord?

The `ImageContent` class in IronWord provides a structured way to manage and customize image attributes such as dimensions and positioning before adding them to DOCX files, ensuring consistency and reducing formatting errors.

How do I maintain the aspect ratio of images when resizing in IronWord?

To maintain the aspect ratio while resizing an image in IronWord, calculate proportional dimensions based on the target size to avoid distortion and create a professional presentation.

What sequence should I follow when setting image properties in IronWord?

It's recommended to set properties in this order in IronWord: first dimensions (width/height), then spacing and margins (DistanceFrom properties), and finally advanced settings like position and text wrapping for comprehensive image layout control.

Ahmad Sohail
全端開發人員

Ahmad是一位擁有C#、Python和網頁技術堅實基礎的全端開發人員。他對構建可擴展的軟體解決方案深感興趣,並喜歡探索設計和功能在現實世界應用中的結合。

...
閱讀更多

準備開始了嗎?

Nuget Downloads 56,401版本:2026.9剛剛發布

即時獲取您的30天試用金鑰
無需信用卡或帳戶建立
C# PDF的NuGet程式庫
通過NuGet安裝

版本: 2026.9

PM > Install-Package IronWord
nuget.org/packages/IronWord/
  1. 在方案總覽中右鍵點擊引用,管理NuGet包
  2. 選擇Browse並搜尋“IronWord”
  3. 選擇套件並安裝
C# PDF DLL
下載DLL

版本: 2026.9

  1. 下載並解壓IronWord到如~/Libs的目錄中
  2. 在Visual Studio方案總覽中右鍵點擊引用。選擇Browse,“IronWord.dll”

授權從$999

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預約您的免費現場演示
Booking Badge

全球數百萬工程師信賴

Iron Software的客戶標誌
獲取您的無義務諮詢
完成以下表單或發郵件至sales@ironsoftware.com
您的詳細資訊將始終保持機密。
全球數百萬工程師信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立