How to Add Image to DOCX in C#
IronWord provides the ImageContent class to insert images (.jpg, .png, .bmp, .tiff, .gif) into DOCX files with customizable properties like width, height, and text wrapping. Use IronWord to add images to Word documents for document automation and report generation.
- Install IronWord via NuGet Package Manager
- Create a new
WordDocumentinstance - Load your image using
ImageContentclass - Add the image to the document using
AddImage() - Save the document as DOCX
-
1Install IronWord with NuGet Package Manager
-
2Copy and run this code snippet.
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# -
3Deploy to test on your live environment
Start using IronWord in your project today with a free trial
Try IronWord
How to Add an Image to DOCX
- Download the latest stable version of IronWord
- Initialize a new Word document
- Define an image object (.bmp, .jpg, .png, or other supported formats)
- Add the image to the document
- Save and export the document file
How Do I Add an Image to DOCX?
Reference an image using its file path. First, instantiate the ImageContent class with the file path as a string. Use the image variable throughout the file to modify properties like width and height. Add the image to the .docx file using the AddImage() function. Export and save the document locally.
The example below adds an image to the document without any parent node. Supported file formats include .jpg, .png, .bmp, .tiff, and .gif. This flexibility lets you work with any common image format.
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");Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Initializing docx file
Dim doc As New IronWord.WordDocument()
' Instantiating image file
Dim image As 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")
Which Image Formats Are Supported?
Supported file formats: .jpg, .png, .bmp, .tiff, and .gif. Each format maintains its quality when inserted. JPEG works best for photographs. PNG supports transparency for logos and graphics. BMP provides uncompressed quality. TIFF suits high-quality print documents. GIF allows simple animations (only the first frame displays in static documents).
Where Is the Image Placed in the Document?
Images are added at the current cursor position by default, without any parent node. For precise positioning, insert images as child elements within paragraphs. This provides better control over text flow and integrates images with your document's structure.
Why Use ImageContent Class?
The ImageContent class manages image properties in a structured way. Modify dimensions, positioning, and formatting before insertion. This approach ensures consistency across your document generation process and applies standard formatting rules throughout your application. The class encapsulates all image-related properties, making code more maintainable and reducing formatting errors.
How Do I Add Images via Stream?
Local or static URL images are easy to add using the previous method. However, applications often work with images from databases, web services, or dynamically generated content. Use the Stream method to add images behind secure APIs requiring authentication.
The example below shows an HTTP client sending authorization tokens to retrieve an authenticated image stream. The stream integrates directly into the document before export. This approach eliminates temporary file storage and improves security for sensitive image data.
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");Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
Imports System.IO
Imports System.Net.Http
Imports System.Threading.Tasks
' initializing docx file
Dim doc As New IronWord.WordDocument()
Using client As 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
Dim authenticatedStream As Stream = Await client.GetStreamAsync("https://api.example.com/secure/image.png")
doc.AddImage(authenticatedStream)
End Using
' Export docx
doc.SaveAs("added-image-via-http-stream.docx")When Should I Use Stream Method?
Use the Stream method when:
- Images are behind secure APIs requiring authentication
- Processing images dynamically from memory
- Working with images stored as binary data in databases
This method works well in enterprise applications where images are stored in document management systems, cloud storage, or generated by image processing services.
What Are the Benefits of Stream Loading?
Stream loading integrates images from authenticated endpoints without saving temporary files. This improves security and performance. Benefits include:
- Reduced disk I/O operations
- No sensitive image caching on disk
- Real-time image processing workflows
- Better memory management for large images
- Flexible image source options
How Can I Modify Image Properties?
IronWord provides comprehensive methods to customize image properties. Adjust these properties before or after adding the image to the document.
Width, Height, and the DistanceFrom* properties default to points (1 inch = 72 points) when assigned directly, as shown below. To work in a different unit, use the corresponding Get/Set overload instead — for example, image.SetWidth(5, MeasurementUnit.Centimeter).| Settings | Description | Example |
|---|---|---|
Width | Horizontal dimension of the image, in points (1 inch = 72 points) | image.Width = 500; |
Height | Vertical dimension of the image, in points (1 inch = 72 points) | image.Height = 300; |
TextWrapBehavior | Text wrapping behavior around the image. Its type is the ITextWrapBehavior interface; no public implementing class or factory is currently documented for constructing a value, so no working example is shown here pending product-team confirmation. | - |
DistanceFromLeft | Spacing measurement from left edge, in points | image.DistanceFromLeft = 10; |
DistanceFromRight | Spacing measurement from right edge, in points | image.DistanceFromRight = 10; |
DistanceFromTop | Spacing measurement from top edge, in points | image.DistanceFromTop = 15; |
DistanceFromBottom | Spacing measurement from bottom edge, in points | image.DistanceFromBottom = 15; |
Position | Spatial placement information (X and Y coordinates) | var pos = new ElementPosition(); pos.SetXPosition(50, MeasurementUnit.Point); pos.SetYPosition(100, MeasurementUnit.Point); image.Position = pos; |
How Do I Customize Width & Height?
Implement custom width and height by altering the aspect ratio. Control how images appear in documents whether maintaining proportions or fitting into specific layout constraints.
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");Imports IronWord
' initializing docx file
Dim doc As New IronWord.WordDocument()
' instantiating image file
Dim image As 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")
What Happens to Aspect Ratio?
Custom width and height values override the original aspect ratio. Stretch or compress images to fit layout requirements like headers, sidebars, or fixed-size containers. Extreme distortion can look unprofessional. To maintain aspect ratio while resizing, calculate proportional dimensions based on your target size.
Which Properties Should I Set First?
Set properties in this order:
- Dimensions (width/height) - foundation of image layout
- Positioning (DistanceFrom properties) - control spacing and margins
- Advanced properties (position, text wrap) - fine-tuning
This approach ensures each property builds logically on previous ones. Some properties interact - text wrapping affects how distance properties work.
Frequently Asked Questions
How can I add an image to a DOCX file using IronWord?
To add an image to a DOCX file using IronWord, initiate a `WordDocument`. Load your image with the `ImageContent` class, configure the image properties like width and height if necessary, and use the `AddImage()` method to insert it into the document.
What image formats does IronWord support for DOCX files?
IronWord supports various image formats including .jpg, .png, .bmp, .tiff, and .gif. This allows you to maintain image quality while inserting different types of images into your DOCX files.
Is it possible to customize image dimensions in IronWord before adding to DOCX?
Yes, you can customize image dimensions by setting the `Width` and `Height` properties of the `ImageContent` class before adding the image to the DOCX file.
How do I handle image insertion from authenticated APIs in DOCX using IronWord?
Use the `Stream` method within IronWord to load images from authenticated APIs. This method allows integrating images directly into DOCX files using secure, real-time processing without needing to save image files temporarily.
What are the advantages of using the `Stream` method for image loading in IronWord?
The `Stream` method enhances security and performance by reducing disk I/O, avoiding temporary file storage, enabling real-time workflows, and improving memory management for large images.
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 is a full-stack developer with a strong foundation in C#, Python, and web technologies. He has a deep interest in building scalable software solutions and enjoys exploring how design and functionality meet in real-world applications.